Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I liked the split method recommended above.
ty
Can you use the OR in the split command to pattern match at the time of the split?
You want the version number to be the last element of the list that split returns, so you could reverse the list, grab the last element, slurp the rest into an array, reverse that array back the right way round again and join the elements with underscores.
There are filenames without "_": 12345.pdf
Yuk, make an @unversioned array and push them into that if you only get one element from split?
#!/usr/bin/perl use strict; use warnings; my @data = qw/12345.pdf 12345_-v1.pdf 12345_Av1.pdf 123456_-v1.pdf 123456_Av1.pdf 123456_Bv1.pdf g05495_1_-v1.pdf zprt0019548_wiper_die-nc_-1.pdf zprt0019548_wiper_die-nc +_-2.pdf zprt0016809_fg_tooling_A2.pdf zprt0016809_fg_tooling_A +3.pdf/; my %hash; my @unversioned; for my $f (sort @data) { my ($version,@key) = reverse split /_/,$f; my $key = join "_",reverse @key; if($key) { $hash{$key} = $version; } else { push @unversioned,$version; } } for my $key (keys %hash) { print $key . '_' . $hash{$key} . "\n"; } print join "\n",@unversioned,q{};
--
Linux, perl, punk rock, cider: charlieharvey.org.uk.

In reply to Re^5: Pattern Matching in Arrays by ciderpunx
in thread Pattern Matching in Arrays by aj.kohler

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 drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 09:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found