Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How can I define arrays using foreach loop

by Anonymous Monk
on Jul 17, 2002 at 08:09 UTC ( #182354=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to define arrays using foreach loop. I know that my syntax is wrong, can you suggest a better way of doing this.

@thearray is created based on user input.

@thearray = (1, 4, 6) my $line = <INFILE>; my @column = split " ", $line; @bigarray = (@array_1, @array_2, @array_3, @array_4,@array_5, @array_6 +); foreach(my $i = 0; $i <= $#thearray; $i++) { push @bigarray[$thearray[$i]], [$line, $column[$thearray[$i]]; }
desired result:

@array_1 = [$line, $column[1]] @array_4 = [$line, $column[4]] @array_6 = [$line, $column[6]]

Replies are listed 'Best First'.
Re: How can I define arrays using foreach loop
by Juerd (Abbot) on Jul 17, 2002 at 08:51 UTC

    my @thearray = (1, 4, 6); my @columns = split ' ', <INFILE>; my @arrays = \(@array_1, @array_2, @array_3, @array_4, @array_5, @arra +y_6); for (@foo) { push @{ $arrays[$_] }, [ $line, $columns[$_] ]; }
    "thearray", "bigarray" and "array_1" are bad names. Think of better ones!

    And use an array of arrays, not symbols that have an index in their name. See perllol, perlreftut, perlref.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.

Re: How can I define arrays using foreach loop
by bronto (Priest) on Jul 17, 2002 at 10:03 UTC

    In @bigarray = (@array_1, @array_2, @array_3, @array_4,@array_5, @array_6) ; you don't get an array of arrays, but a plain array with (a copy of) the elements of @array_#s. You should change it to:

    @bigarray = (\@array_1, \@array_2, \@array_3, \@array_4, \@array_5, \@array_6) ;

    That is, @bigarray contains references to the @array_#s.

    You are using a foreach syntax for a for cycle. If you want to stick on for, change your foreach in for, and then:

    push @{$bigarray[$thearray[$i]]}, [$line, $column[$thearray[$i]];

    Since you are dereferencing $bigarray[$thearray[$i]], you are really pushing into @array_$i

    If you want to use foreach you could just change your loop to

    foreach my $i (@thearray) { push @{$bigarray[$i]}, [$line, $column[$i]]; }

    Whatever you prefer, it's a matter of style :-)


    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;

Re: How can I define arrays using foreach loop
by Util (Priest) on Jul 17, 2002 at 14:04 UTC

    Juerd and bronto are completely correct.

    If you mean for the user input 1 (found in @thearray) to indicate @array_1 and the first element of @column, then you must subtract 1, since Perl arrays are zero-based.

    Also, if @array_1 and its siblings exist solely to populate @bigarray, then you could replace them with [], the empty anonymous array constructor, as I have done (and then commented out) below.

    Here is a working code sample:
    #!/usr/bin/perl -W use strict; my (@array_1,@array_2,@array_3,@array_4,@array_5,@array_6); my @big = \(@array_1,@array_2,@array_3,@array_4,@array_5,@array_6); #my @big = map [], 1..6; my @thearray = (1, 4, 6); #my $line = <INFILE>; my $line = 'able baker charlie roger fox dog'; my @column = split " ", $line; push @{$big[$_]}, [$line, $column[$_]] foreach map {$_-1} @thearray; use Data::Dumper; #print Data::Dumper->Dump([\@big], ['*big']); print Data::Dumper->Dump( [ \(@array_1,@array_2,@array_3,@array_4,@array_5,@array_6)], [qw(*array_1 *array_2 *array_3 *array_4 *array_5 *array_6)], );
    This is the (reformatted) output:
    @array_1 = ( [ 'able baker charlie roger fox dog', 'able' ] ); @array_2 = ( ); @array_3 = ( ); @array_4 = ( [ 'able baker charlie roger fox dog', 'roger' ] ); @array_5 = ( ); @array_6 = ( [ 'able baker charlie roger fox dog', 'dog' ] );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://182354]
Approved by schumi
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2016-09-25 09:40 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (468 votes). Check out past polls.