Here's how you can extract the data you want and output it in the sort order you show:
$ perl -Mstrict -Mwarnings -le ' my @xml_paths = qw{ 20120824/ABC/input/daily/11337111.xml 20120710/ABC/input/daily/11337101.xml 20120330/ABC/input/daily/11337111.xml 20130614/ABC/input/daily/11337890.xml 20130120/ABC/input/daily/11337111.xml 20120206/ABC/input/daily/11337111.xml 20121005/ABC/input/daily/11337890.xml 20130110/ABC/input/daily/11337111.xml 20120923/ABC/input/daily/11337111.xml 20130416/ABC/input/daily/11337101.xml 20120404/ABC/input/daily/11337111.xml 20130211/ABC/input/daily/11337111.xml }; my %latest; for (@xml_paths) { m{(.+\/)([^\/]+)}; $latest{$2} = $1 if ! $latest{$2} || ($latest{$2} cmp $1) < 0; } print for sort map { "$latest{$_}$_" } keys %latest; ' 20130211/ABC/input/daily/11337111.xml 20130416/ABC/input/daily/11337101.xml 20130614/ABC/input/daily/11337890.xml
You're still going to have to read your data from a file. Using the code you've posted will cause you no end of problems, particularly when you write scripts of more than half a dozen lines. I recommend you change:
#!/usr/bin/perl open(INFO,"$ARGV[0]"); @array=<INFO>; close (INFO)
To something closer to this:
#!/usr/bin/perl use strict; use warnings; use autodie; open my $input_fh, '<', $ARGV[0]; my @xml_paths = <$input_fh>; close $input_fh;
"@array" is a completely meaningless variable name: the "@" already tells us it's an array; "array" adds nothing useful. "@xml_paths", on the other hand, gives a pretty clear indication of what it is (even when it appears many lines later after "my @xml_paths = <$input_fh>;" has scrolled off the top of the screen).
For the other recommended changes, see strict, warnings, autodie and open.
If you want to write your own custom die messages, such as those shown in the open documentation, you don't need the "use autodie; line; however, if you do this, make sure you've checked the results of all the functions that autodie checks and written custom messages for all of them. Obviously, using the autodie pragma is a lot less work for you.
-- Ken
In reply to Re: sort based on last and first value
by kcott
in thread sort based on last and first value
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |