nashkab has asked for the wisdom of the Perl Monks concerning the following question:

I have a file which looks like this:----

ENDPOINT DISTRIBUTION_ID STATUS VMW `1781183799.31152' IC--- WX `1781183799.32206' BN1 `1781183799.32206' BN2 `1781183799.32206' CAU `1781183799.32206' IC--- BN3 `1781183799.32206' IC--- BN4 `1781183799.32206' IC--E BN5 `1781183799.32206' IC--- BN6 `1781183799.32206' IC--- BN7 `1781183799.32206' IC---

I want to read this file into an array :--

open(FILE,"file.txt") my @Records = <FILE>;

now i want to sort this array using sort function:
i have used

my @Records = sort {lc($a) cmp lc($b)} @Records and my @Records = sort {$a cmp $b} @Records my @Records = sort { $a[0] cmp $b[0] } @Records

But its not giving me desired result
I want the file like this:

BN1 `1781183799.32206' BN2 `1781183799.32206' BN3 `1781183799.32206' IC--- BN4 `1781183799.32206' IC--E BN5 `1781183799.32206' IC--- BN6 `1781183799.32206' IC--- BN7 `1781183799.32206' IC--- CAU `1781183799.32206' IC--- VMW `1781183799.31152' IC--- WX `1781183799.32206'

I want the sort based on the first column

Edit: g0n code tags and formatting

Replies are listed 'Best First'.
Re: Need Help! Two Dimentional Array Sorting
by ww (Archbishop) on Jan 03, 2008 at 04:17 UTC
    but, since OP has asked for perlish help, here's part of the post, edited for rendering and statement terminators:
    (add shebang to taste) use strict; use warnings; open(FILE,"file.txt"); my @Records = <FILE>; @Records = sort {$a cmp $b} @Records; for my $record(@Records) { print "$record \n"; }

    Between them, strict and warnings would have sorted out most of your problems, including missing semi-colons.

    Tho it "works," the above code is NOT a good piece of work; read perldoc -f open and perldoc -f sort with special attention to testing your open (to make sure it works/find out why it didn't work).

    You may find help in threads such as #146936 using the Montastery's "Search" function (top of the page).

    Update: Added output statement; rephrased second para after the code for clarity.

Re: Need Help! Two Dimentional Array Sorting
by McDarren (Abbot) on Jan 03, 2008 at 04:03 UTC
    I wouldn't even bother using Perl for this.

    In the *nix world...

    cat file.txt | sort -k1

    Or in winders-land...

    type file.txt | sort /+1

    Note that because you want to sort by the first "field", you don't even need to specify the sort field, as both winders and *nix sort do that by default. So just piping the file through sort is sufficient.

      I wouldn't even bother using Perl for this.

      In the *nix world...

      Agreed, but it's not like using perl for this is so bad (esp. for the poor M$ users who don't know/have the unix tools):

      perl -e "print sort <>" file.txt
      But -- curious discovery -- depending on the file content, this can produce somewhat different output from the unix "sort file.txt": unix sort puts empty lines first, whereas perl's sort puts them between the lines that start with tab and the ones that start with space or other characters ("\x0a" and "\x0d\x0a" come after "\x09" and before "\x20"). That is, in its simplest form, the perl version takes line termination characters into account for sorting, but unix sort does not. Doing a proper emulation of unix sort with perl takes a bit more work. So far, this is the "best" I can do (it does work -- it's just a tad ugly):
      perl -e "print sort {chomp($x=$a); chomp($y=$b); $x cmp $y} <>" file.t +xt
      (I'm not sure, but I think it would work using double-quotes in the standard M$ shell, whereas a unix shell (e.g. bash for windows) needs single quotes.)
        perl -e "print sort {chomp($x=$a); chomp($y=$b); $x cmp $y} <>" file.txt

        The following is also a bit challenged in the beauty stakes but avoids the repetitive chomp'ing of the lines. Tested under both M$ shell (with double quotes) and Solaris ksh (with single-quotes).

        perl -e "print map{qq{$_$/}} sort map{chomp; $_} <>;" file.txt

        I hope this is of interest.

        Cheers,

        JohnGG

Re: Need Help! Two Dimentional Array Sorting
by starX (Chaplain) on Jan 03, 2008 at 04:18 UTC
    Hi, and welcome to perlmonks. Before moving on to the main questions, I'll get the mandatory "please use code tags" thing out of the way for displaying code. It helps everything look perty.

    Also, you might try dropping  use strict; use warnings; at the top of everything you do. I *think* that would have helped you spot the main problem a little bit sooner.

    That all being said, I tried the following, and I got the results you were expecting.

    use strict; use warnings; my @Records; open(FILE,"file.txt"); while (<FILE>){ @Records = <FILE>; } @Records = sort {$a cmp $b} @Records; # and now to test. my $i = 0; while ($i <= $#Records){ print "$Records[$i]\n"; $i++; }
    Of course you say you're trying to sort a two dimensional array in the subject, but this array isn't two dimensional, so I suspect you're not telling the whole story.

    Cheers!

Re: Need Help! Two Dimentional Array Sorting
by CountZero (Bishop) on Jan 03, 2008 at 07:10 UTC
    For all your sorting needs beyond a simple straightforward sort, have a look at Sort::Key. Also Sort::Key::Natural is worth a look as it solves the problem of sorting "mixed keys" (which contain both alfabetic and numeric characters).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James