Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Sort and push

by johngg (Canon)
on May 15, 2009 at 10:46 UTC ( #764238=note: print w/replies, xml ) Need Help??


in reply to Sort and push

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.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2023-12-09 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (38 votes). Check out past polls.

    Notices?