Re: read 2 array simultaneously
by jZed (Prior) on Jan 12, 2005 at 02:37 UTC
|
In the first place, you should be using strict and warnings. If you had, you would have been warned that you are trying to use commas in a qw() list. In the second place, what you really want to do (to get the output you desire) is loop through the positions in the list and not have two loops. So:
#!perl -w
use strict;
my $x=1;
my $y=2;
my @a=qw(1 2 3);
my @b=qw(7 8 9);
for my $pos(0..2) {
print "$x:$a[$pos] $y:$b[$pos]\n";
}
| [reply] [d/l] |
Re: read 2 array simultaneously
by Zaxo (Archbishop) on Jan 12, 2005 at 02:38 UTC
|
The qw operator will see those arrays as a single element each. seperate with white space. You don't want a nested loop, you want to look at both arrays in a single loop over an index. I'll assume they are the same length.
my $x = 1;
my $y = 2;
my @a = qw(1 2 3);
my @b = qw(7 8 9);
for (0 .. $#a) {
print "$x:$a[$_] $y: $b[$_]\n";
}
| [reply] [d/l] |
Re: read 2 array simultaneously
by Ovid (Cardinal) on Jan 12, 2005 at 03:11 UTC
|
And in the spirit of TIMTOWTDI, the Perl6 inspired ...
sub zip {
my ($ref1, $ref2) = @_;
my @zip;
while (@$ref1 || @$ref2) {
push @zip => shift @$ref1 || undef;
push @zip => shift @$ref2 || undef;
}
return @zip;
}
my $x = 1;
my $y = 2;
my @a=qw(1 2 3);
my @b=qw(7 8 9);
my @zip = zip(\@a, \@b);
while (my ($item, $element) = splice @zip => 0, 2) {
print "$x: $item $y: $element\n";
}
Better written as:
for zip(@a,@b) -> $item, $element {
say "$x: $item $y: $element";
}
But you'll have to wait a bit for that syntax to work :)
| [reply] [d/l] [select] |
|
|
Just for my own edification, do you need to do "shift @$ref1 || undef"? shift will return undef if there are no elements in the array.
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
| [reply] |
|
|
| [reply] |
Re: read 2 array simultaneously
by ercparker (Hermit) on Jan 12, 2005 at 02:44 UTC
|
my $x=1;
my $y=2;
my @a = qw(1 2 3);
my @b = qw(7 8 9);
my $count = 0;
foreach (@a) {
print "$x:$_ $y:$b[$count];\n";
$count++
}
| [reply] [d/l] |
Re: read 2 array simultaneously
by Tanktalus (Canon) on Jan 12, 2005 at 03:53 UTC
|
There are more than enough Ways To Do It already presented - I just want to point out that the first three Ways were all assuming that the two arrays were of the same size. What happens when the arrays are different lengths depends on which solution you use. Ovid's solution was the most work, but if it is possible that the arrays are different sizes, that solution is the most robust, handling all cases quite well. Note that in that solution, the shorter array, if there is one, will be treated as if it were filled with undef's - which your code would have to handle, e.g.,
printf "%s:%s %s:%s\n",
$x, defined $item ? $item : '<undef>',
$y, defined $element ? $element : '<undef>';
| [reply] [d/l] |
|
|
Well, the first reply above was explicitly coded to the OP's questions and the second said it assumed equal length, so they were at least thinking about it but the undef handling is a good point. TIMTOWDI is fun and good practice. How about this? Handles different sized arrays and prints undefs nicely, too.
use strict;
my @a = qw( 1 2 3 );
my @b = qw( 7 8 9 10 );
my ($x, $y) = (1, 2);
for (my $i = 0; $i < @a || $i < @b; ) {
printf( "$x: %s $y: %s\n",
map { defined $_ ? $_ : 'undef' } ( $a[$i], $b[$i++] ) );
}
update: side effect -- will extend the shorter array with undefs. Could copy @a and @b before handing to the loop if this was a concern.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] [d/l] |
Re: read 2 array simultaneously
by holli (Abbot) on Jan 12, 2005 at 06:56 UTC
|
this one uses map and copes with variable array length:
print map
{
"$x:" . ($a[$_] ? $a[$_] : "undef") . " " .
"$y:" . ($b[$_] ? $b[$_] : "undef") . ";" .
"\n"
}
(0..($#a < $#b ? $#b : $#a));
| [reply] [d/l] |
Re: read 2 array simultaneously
by Roy Johnson (Monsignor) on Jan 12, 2005 at 14:49 UTC
|
| [reply] |
Re: read 2 array simultaneously
by gube (Parson) on Jan 12, 2005 at 10:26 UTC
|
my $x=1;
my $y=2;
my @a=qw(1 2 3);
my @b=qw(7 8 9);
for my $y(0..$#a) {
print "$x:$a[$y] $y:$b[$y]\n";
}
The output is
1:1 2:7
1:2 2:8
1:3 2:9
Regards,
Gubendran.L | [reply] [d/l] |
Re: read 2 array simultaneously
by gube (Parson) on Jan 12, 2005 at 10:39 UTC
|
my $x=1;
my $y=2;
my @a=qw(1 2 3);
my @b=qw(7 8 9);
my $i=0;
for (@a){
print "$x:$a[$i] ";
print "$y:$b[$i] ";
print "\n";
$i++;
}
1:1 2:7
1:2 2:8
1:3 2:9
| [reply] [d/l] |