#! perl -slw use strict; use Data::Dump qw[ pp ]; my %categs; push @{ $categs{ $_->[2] } }, $_->[1] while @{ $_ = [ split ' ', ] }; pp \%categs; __DATA__ 31 John Consultation Charges 100 4/1/2012 10:39 805 Kennedy Consultation Charges 150 4/1/2012 11:17 805 Kennedy C-Reactive Protein 170 4/1/2012 11:56 805 Kennedy Complete Blood Count 150 4/1/2012 11:56 805 Kennedy Malarial 175 4/1/2012 11:56 805 Kennedy Mantoux Test 100 4/1/2012 11:56 805 Kennedy AZIBACT 1 MG SYP 28 4/1/2012 13:27 805 Kennedy FALCINILLE DRY SYP 105.15 4/1/2012 13:27 891 Ruth Consultation Charges 150 4/1/2012 12:05 891 Ruth C-Reactive Protein 170 4/1/2012 12:47 891 Ruth Complete Blood Count 150 4/1/2012 12:47 891 Ruth Mantoux Test 100 4/1/2012 12:47 891 Ruth X-Ray Chest 150 4/1/2012 12:47