perlperlscript

perl assign variables to an array with relationship


Please advice how to pass 3 variables in an array with relation.

@item = ($a , $b , $c);
@record = push(@array, @item);

I want to assign value in a @array so that if I look for any instance I should get value of all a,b,c.

Is there any way apart from comma to assign a value in array. like $a:$b:$c or $a>$b>$c I need this because i am want to grep 1 record(a) and get (a:b:c)

@array1 = grep(!/$a/, @array);

expected output should be a:b:c 

Thanks,


Solution

  • The question is not very clear. Maybe you should rephrase it. However, I understand you want an array with groups of three elements.

    You might want to use array references.

    @item = ($a , $b , $c);
    push(@array, \@item);
    

    or

    $item = [$a , $b , $c];
    push(@array, $item);
    

    Also, pushwon't return an array as you expect. Perldoc says:

    Returns the number of elements in the array following the completed "push".

    Now if you want to filter these groups of three elements, you can do something like that:

    my @output = ();
    L1: foreach ( @array ){
        L2: foreach( @$_ ){
            next L1 if $_ eq $a; 
        }
        push @output, $_;
    }
    

    Please note that if you want an exact match you should use the eq operator instead of a regex...