Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Sort and push

by sandy1028 (Sexton)
on May 15, 2009 at 06:36 UTC ( #764194=perlquestion: print w/replies, xml ) Need Help??

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

How to sort all the values before pushing to an array. The values in @arr should be in sorted order after all the values are pushed into array.
foreach $data(@datas){ @data=split("/",$data); $data[4] =~ s/\.prn//ig; push(@arr,"$data[0]/$data[1]"); }

Replies are listed 'Best First'.
Re: Sort and push
by targetsmart (Curate) on May 15, 2009 at 06:49 UTC
    it is very easy; use sort after the for loop
    you have to tell us on what kind of data you are trying to operate, it helps us to tell you what kind of sort you have to use
    what $data[0] and $data[1] contain?

    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Sort and push
by johngg (Canon) on May 15, 2009 at 10:46 UTC

    Since you are spliting and concatenating on forward slashes it looks like you might be dealing with *nix paths. Like Bloodnok, I'm not sure why you are changing element 4 then not using it. The following script forms @arr four different ways as I'm not sure what you intend:-

    • Just the first two elements as in your code.
    • The first three so that you get two path elements.
    • All elements with the fourth element changed as in your code if it contains '.prn' (I have anchored to the end of string in the assumption that it is an extension).
    • As above but changing the last element instead of the fourth to cope with paths with different depths.

    use strict; use warnings; my @data = qw{ /path/some/random/file /home/mary/projectA/reports/rep1.prn /usr/man/man1/brewcoffee.1 /home/fred/projects/cv.prn /home/bill/work/prog.c }; my @arr = sort map { join q{/}, ( split m{/} )[ 0, 1 ] } @data; print qq{$_\n} for @arr; print q{-} x 20, qq{\n}; @arr = sort map { join q{/}, ( split m{/} )[ 0 .. 2 ] } @data; print qq{$_\n} for @arr; print q{-} x 20, qq{\n}; @arr = sort map { $_->[ 4 ] =~ s{.prn$}{}i ; join q{/}, @$_ } map { [ split m{/} ] } @data; print qq{$_\n} for @arr; print q{-} x 20, qq{\n}; @arr = sort map { $_->[ -1 ] =~ s{.prn$}{}i ; join q{/}, @$_ } map { [ split m{/} ] } @data; print qq{$_\n} for @arr; print q{-} x 20, qq{\n};

    The output.

    /home /home /home /path /usr -------------------- /home/bill /home/fred /home/mary /path/some /usr/man -------------------- /home/bill/work/prog.c /home/fred/projects/cv /home/mary/projectA/reports/rep1.prn /path/some/random/file /usr/man/man1/brewcoffee.1 -------------------- /home/bill/work/prog.c /home/fred/projects/cv /home/mary/projectA/reports/rep1 /path/some/random/file /usr/man/man1/brewcoffee.1 --------------------

    I hope I have guessed correctly and this is of some use to you.



Re: Sort and push
by Bloodnok (Vicar) on May 15, 2009 at 09:48 UTC
    As targetsmart says, you should define the nature of the sort you require.

    That being said, using the default (alphbetic) sort (and assuming @datas & amp; @arr are the source & result arrays respectively), you'd use something like...

    my @arr = sort @datas;
    BTW, you do realise that, within the loop in your snippet, you modify the fifth element of @data and then only use the first 2 elements of said array - by pushing a concatenation of them onto the result array.

    A user level that continues to overstate my experience :-))
Re: Sort and push
by vinoth.ree (Monsignor) on May 15, 2009 at 14:10 UTC

    Select any one of the sorting method from the bellowed list and do sorting

    @sorted = sort { $a <=> $b } @not_sorted # numerical sort @sorted = sort { $a cmp $b } @not_sorted # ASCII-betical sort @sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764194]
Approved by targetsmart
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2023-09-25 17:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found