#!/usr/bin/perl -w use strict; open(TEST, 'test.txt') || die "Couldn't open it"; print "\nFile Opened\n\n"; my $i=1; my %list; while(){ chomp($_); $_ = /(\w*)\s?(\w*)\n?/; print "$i : $2\,$1\n"; %list = ("$i" => ["$1","$2"]); $i+=1; } foreach $a(sort(keys(%list))){print "$list{$a}[1]\,$list{$a}[0]\n"} close(TEST); die "\n\nFile Closed\n";