SY...
Expanding on madbombX 's cautionary observation, above: the Monastery is a treasury of knowledge and helpful educators but it is NOT a shop where, on request, we write code to satisfy your specific needs and for free, at that. Many if not most of us make our living writing code and while we do try to offer -- freely -- advice and information, your blunt request that we provide -- for free -- a particular script in a specified number of lines is (perhaps unintentionally?) offensive.
You may also profit from noting this frequently offered admonition: "Show some effort; show us what you've tried!" | [reply] |
Like a lot of monks, I am not one to completley write code for someone. Especially since it sounds like an assignment (since you are specifying the exact number of lines you need it in). However, to open the file up, check out IO::File, read in the line, put the vars into an array and then sort them: for my $line (<FH>) {
my @unsorted = split / /, $line;
# sort ascending
my @sorted = sort @unsorted;
# sort descending
my @desc_sort = reverse @sorted;
}
Check out the pages on sort, reverse, and split for more information. There are certainly shorter ways to do this, but you need to understand what's going on before you can condense it. | [reply] [d/l] |
Watch out: sort sorts normally "in standard string comparison order" which is NOT numerically. In order to sort numerically, you have to use an explicit sort routine.
With your expamle input file, this won't make a difference though, as it sorts the same numerically AND alphabetically.
-- Hofmator
Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] |
If you're on *ix, use the system sort utility (sort -n). If you're on Windows, its sort command doesn't seem to have a numerical option (see sorting in XP).
As an aside, you'll likely get much better response from the Monks if you, first, show what you've tried (in this case, seemingly, nothing), and, second, don't ask for code.
Oh, and a hint:
@out = sort { $a <=> $b } @in; # to sort array @in numerically, and pu
+t it into @out.
emc
At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.
—Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
| [reply] [d/l] |