in reply to Re^2: Making a hash with lists
in thread Making a hash with lists
Nice, but the zip function you postedappends two undefs onto the end of the result set. Use $# to get last indexes.
my @a = 1..3; my @b = qw( a b c ); my @foo = orig_zip( \@a, \@b ); my @bar = fixed_zip( \@a, \@b ); print Data::Dumper->Dump( [ \@foo, \@bar ], [ 'original', 'fixed' ] ); sub orig_zip { my( $r1, $r2 ) = @_; map { $r1->[ $_ ], $r2->[ $_ ] } 0 .. ( @$r1 > @$r2 ? @$r1 : @$r2 ); } sub fixed_zip { my( $r1, $r2 ) = @_; map { $r1->[ $_ ], $r2->[ $_ ] } 0 .. ( $#$r1 > $#$r2 ? $#$r1 : $#$r2 ); } __END__ $original = [ 1, 'a', 2, 'b', 3, 'c', undef, undef ]; $fixed = [ 1, 'a', 2, 'b', 3, 'c' ];
TGI says moo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Making a hash with lists
by BrowserUk (Patriarch) on Jun 27, 2008 at 18:26 UTC |