Hi all! I am sill learning Perl, and have just installed ActivePerl on my Windows machine.
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...)
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;
I know it must be VERY simple, but I am just confusing myself more!!! :(
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.