#!/usr/bin/perl -w use strict; use Data::Dump qw(pp); my %Month2Index = (January => 0, Febuary =>1, March =>2); my $january =<; #first line - throw away in real thing my $month_name = (split ' ',$comment)[-1]; my $month_index = $Month2Index{$month_name}; process_monthly_file ($file, $month_index); } sub process_monthly_file { my ($file, $month_index) = @_; while (<$file>) { s/^\s*//; #remove leading spaces # this is a space separated format, but we # want the last column and all of the columns # before that should be "squished into one column" # one way is to reverse the line, limit the # split and then reverse again. my $reversed = reverse $_; my ($data, $name) = split (' ', $reversed,2); # note: the split does an implicit "chomp" $data = reverse $data; $name = reverse $name; # if this name not seen before, create a new # hash entry with a blank array, here just 4 # columns (jan,feb,mar,april) $spreadsheet{$name} ||= [0,0,0,0]; #or perhaps... $spreadsheet{$name} ||= [qw(NA NA NA NA)]; #now enter the data into correct column # @{$spreadsheet{$name}}[$month_index] = $data; } } print pp \%spreadsheet; __END__ prints: { "A. Paul" => [300004, 0, 300004, 0], Jason => [600000, 0, 0, 0], "Kelly H" => [459000, 0, 459000, 0], "Mayur Pandey" => [40000, 0, 40000, 0], "Pratap S" => [0, 0, 349000, 0], "Ryan M" => [349000, 0, 0, 0], "Senthl V R" => [0, 0, 600000, 0], }