Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Sort and push by johngg
in thread Sort and push by sandy1028

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2023-12-03 14:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (20 votes). Check out past polls.

    Notices?