#!/usr/bin/perl -w use strict; my $seen = 0; while () { chop; my ($id, $maker, $make) = split(",", $_); if ($id != $seen) { print "$id. $maker\n"; print " - $make\n"; $seen = $id; } else { print " - $make\n"; } } __DATA__ 1,gm,saturn 1,gm,chevy 1,gm,caddy 2,ford,taurus 2,ford,escort 3,vw,bug 3,vw,jetta 3,vw,phaeton #### my @carmakers = ( \%thismaker, \%thismaker, \%thismaker ); #### my %thismaker = ('thismaker' => 'gm', 'makes' => \@makes); #### my @makes = (\%thismake, \%thismake, \%thismake); #### my %thismake = ('thismake' => 'saturn'); my %thismake = ('thismake' => 'chevy'); my %thismake = ('thismake' => 'caddy'); #### #### my $seen = 0; my @carmakers; while () { chop; my ($id, $maker, $make) = split(",", $_); if ($id != $seen) { my %maker; $maker{'thismaker'} = $maker; my @makes; my %make = ('thismake' => $make); push(@makes, \%make); $maker{'makes'} = \@makes; push(@carmakers, \%maker); # problem1 $seen = $id; } else { my %make = ('thismake' => $make); push(@makes, \%make); # problem2 $maker{'makes'} = \@makes; } } #### while (looping over something) { if (cond1) { create @foo if a certain condition is met do a bunch of things } else { do something to foo already declared do a bunch of other things } }