iamrobj has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to open a text file with multiple lines containing names and phone numbers. They are split by the pipe symbol "|".
I am trying to open the file and sort the file by the phone number (second part in the array), but still keep the name before the number...
Zoe|123-4567
Andrew|123-4568
Ideally, I want to open the file, read contents, sort, write them again, then close. Can I do this? Do I have to write to a new file? Here is what I am doing (and it is NOT working...)
I know it must be VERY simple, but I am just confusing myself more!!! :(my $whatever; my @sortedarray; open(SORTED, ">sorted.txt"); open(TOSORT, "tosort.txt"); while ($whatever = <TOSORT>) { chomp $whatever; my @sortedarray = split(/\|/, $whatever); #@sortedarray = sort($sortedarray[1]); print SORTED "$sortedarray[0]|$sortedarray[1]\n"; } close(TOSORT); close(SORTED); print "done!"; exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sorting arrays...
by dws (Chancellor) on Jul 12, 2003 at 21:58 UTC | |
by iamrobj (Initiate) on Jul 12, 2003 at 22:43 UTC | |
by dws (Chancellor) on Jul 12, 2003 at 23:31 UTC | |
|
Re: sorting arrays...
by Abigail-II (Bishop) on Jul 12, 2003 at 22:34 UTC | |
by dws (Chancellor) on Jul 12, 2003 at 23:54 UTC | |
by Abigail-II (Bishop) on Jul 13, 2003 at 00:24 UTC | |
by demerphq (Chancellor) on Jul 13, 2003 at 01:25 UTC | |
|
Re: sorting arrays...
by bobn (Chaplain) on Jul 12, 2003 at 22:08 UTC | |
|
Re: sorting arrays...
by artist (Parson) on Jul 12, 2003 at 21:52 UTC | |
by iamrobj (Initiate) on Jul 12, 2003 at 22:07 UTC | |
by artist (Parson) on Jul 12, 2003 at 22:33 UTC | |
by iamrobj (Initiate) on Jul 12, 2003 at 22:40 UTC |