Or with some explicit dereferencing:for (keys %conductordata){ print OUT @{$conductordata{$_}}; }
Which only returns the first and second item, to be safe.for (keys %conductordata){ my $ref = $conductordata{$_}; print OUT $ref->[0..1]; }
Hope this helps,
Jeroen
"We are not alone"(FZ)
You can read more about it in perlref.
Apparently not what Gremlin wanted to know. Gremlin, what exactly is yer problem? Just the thing busunsl pointed out, probably....
I was asked for some deeper explanation for the hash overwrite. Well, It goes a bit deep. You'd probably want to read perldata on it. Perl differentiates between scalars, lists and hashes. So once you told the compiler a certain name (GLOB) represents a hash, it stays a hash. It even can coexist with a scalar by the same name!! (very confusing at first, but handy. It means you can say 'print $data for my $data (@data);'.)
Having said this, a call to %some_hash always represents the whole hash. If you want an element, use $some_hash{element}. In your code, you assign to the whole hash in %conductordata = ("conductor$i"=>....... This results in a complete fresh hash, with only one element, but that in each iteration of your loop!
All you have to do, is to assign to an one element each time. You only have the get the element before the '=': $conductordata{"conductor$i"} = [@elements].
Moreover, your data fit conceptually into either a nested hash or a two-dimensional array:
Clean this up a little bit:$number=$values{"number"}; my $con_data=[]; for ($i=1 ;$i<=$number ; $i++){ push @$con_data, [$values{"radius$i"}, $values{"length$i"}]; }
my @numbers = grep{ s/radius(\d+)/$1/ } keys %values; $con_data[$i] = [$values{"radius$i"}, $values{"length$i"}] for my $i ( +@numbers); #then print: print OUT @$_ for (@$con_data);
In reply to Re: Printing Hash of Arrays
by jeroenes
in thread Printing Hash of Arrays
by Gremlin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |