Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Sorting files you Have read

by swampyankee (Parson)
on Nov 16, 2006 at 21:24 UTC ( #584610=note: print w/ replies, xml ) Need Help??


in reply to Sorting files you Have read

First suggestion: use your system's sort routine. While I think the *ix sort is better than the Windows sort, both are reliable, and quite fast.

As for sorting a list in Perl:

use strict; # a good idea use warnings; # and another my $input = 'myinput.txt'; open(my $in, "<", $input) or die "Could not open $input because $!\n"; @unsorted = <$in>; chomp(@unsorted); # get rid of end-of-record markers close($in); @sorted = sort @unsorted;

will sort the @unsorted array, based on the entire string, with the output going to @sort. Note that this requires a copy of the entire file be in memory, which may be a Bad Idea. There are several tutorials, for example, here and here. And of, course, here.

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.


Comment on Re: Sorting files you Have read
Download Code
Re^2: Sorting files you Have read
by brusimm (Pilgrim) on Nov 16, 2006 at 23:07 UTC
    Hey swampyankee, thank you, but when I run the script, I get the following

    Global symbol "@unsorted" requires explicit package name at one-c.pl line 6.
    Global symbol "@unsorted" requires explicit package name at one-c.pl line 7.
    Global symbol "@sorted" requires explicit package name at one-c.pl line 10.
    Global symbol "@unsorted" requires explicit package name at one-c.pl line 10.
    Execution of one-c.pl aborted due to compilation errors.

    I am on an XP machine, if that has any bearing, but would there be something else amiss with my installation?

    The reason i ask is my first responder suggested some code and that looked like it ran successfully, but did not translate any result to the screen or a file.
    Now your code returns errors... hmm

    Any other thoughts?

      I cleverly forgot to declare @unsorted and @sorted. This is why one should (a) test code before posting and (b) use strict; and use warnings;.

      They do wonders to prevent things like:

      @array = (1,2,3,4,5,6,7,8,9); $sum+= $_ foreach @array; $mean = $sum/scalar(@aray);

      (Yes, I know scalar(@aray) is 0.)

      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.
Re^2: Sorting files you Have read
by GrandFather (Cardinal) on Nov 16, 2006 at 23:16 UTC

    If you are going to use strict; use warnings; (and it is a really really good idea), then you really really need to declare your variables: ;)

    use strict; # a good idea use warnings; # and another my $input = 'myinput.txt'; open(my $in, "<", $input) or die "Could not open $input because $!\n"; my @unsorted = <$in>; chomp (@unsorted); # get rid of end-of-record markers close ($in); my @sorted = sort @unsorted;

    DWIM is Perl's answer to Gödel
      Thank you, everyone
      This points me in the right direction
      I used to do IDL and Fortran (About 7 years ago..)
      but this seems harder. Sheesh.. is it me being out of the programming loop so long, or is see-nellity setting in?
      Maybe you shouldn't answer that!!
      Take care.

        You're OK. Perl is just different from what most people are used to. e.g. my CS modules at university (about 3 years ago) mostly used Java, and I have mostly hobby experience of C/C++, but I'd probably feel like you do now if I suddenly tried to do enterprise Java, use C++ templates, use C# or even use Perl in any meaningful way (my main work language is Delphi, although I do support some Perl projects I didn't develop).

        How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
        Robot Tourist, by Ten Benson

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://584610]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-10-02 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (63 votes), past polls