#!/usr/bin/perl -w use strict; my ($key); # Collect the data my $data = {}; while (<>) { chomp; my @row = split /,/; next unless @row; $key = shift @row; # the following is the most important line in the program # understand it and you've got it! push @{$data->{$key}}, [ @row ]; } # Uncomment these lines if you have Data::Dumper and you want to see # what the data structure looks like # use Data::Dumper; # $Data::Dumper::Terse = 1; # print Dumper($data); print "Total count\n"; for $key (sort keys %$data) { print " $key has ", scalar @{$data->{$key}} , " entries\n"; } print "Second and third items of all QTYs\n"; for $key (@{$data->{QTY}}) { print " $key->[0], $key->[1]\n"; } print "Third items in HDRs\n"; for $key (@{$data->{HDR}}) { print " $key->[1]\n"; } print "4th and 5th items in OTIs\n"; for $key (@{$data->{OTI}}) { print " $key->[2], $key->[3]\n"; }