in reply to array of arrays
The "tricky" syntax part is that "extra {}" in @{$hash{$id}} is necessary when using a sub sub-scripted variable (ie just @$hash{$id} won't work! The extra {} tells @ what to operate upon.#!/usr/bin/perl -w use strict; my %hash; while (<DATA>) { my($id,$value)=split; push @{$hash{$id}}, $value; } foreach my $id (sort{$a<=>$b} keys (%hash)) { print "id=$id values=@{$hash{$id}}\n"; } #prints: #id=1 values=3 2 10 #id=2 values=5 6 #id=3 values=4 __DATA__ 1 3 1 2 1 10 2 5 2 6 3 4
I don't know if this is some sort of homework problem?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: array of arrays
by Limbic~Region (Chancellor) on Jun 29, 2009 at 13:42 UTC | |
by Marshall (Canon) on Jun 29, 2009 at 14:16 UTC | |
by Limbic~Region (Chancellor) on Jun 29, 2009 at 14:36 UTC | |
by Marshall (Canon) on Jun 29, 2009 at 15:14 UTC |