CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
search issue

 



aceall
Novice

Jul 11, 2010, 10:33 PM

Post #1 of 2 (605 views)
search issue Can't Post

hey,

I am working on processing large number of files in a directory ..These files are of this format ..


Code
abc-number 
abc-def-number
abc-ghi-number


I would like to sort these files according to the number that is
displayed in the end. If I use wildcard after first word, then it searches
for all the other variants as well.

Like search for abc, i get results of abc-def and abc-ghi as well.

Using delimiter ($) by the end of the string will prevent me from sorting the
number that occurs by the end of it.

Let me know if there is any workaround to search for the exact word-number to
start processing.


BillKSmith
Veteran

Jul 12, 2010, 6:33 AM

Post #2 of 2 (596 views)
Re: [aceall] search issue [In reply to] Can't Post

Extract the number with a regular expression

See perldoc -q sort for Schwartzian Transform


Code
  

use strict;
use warnings;
my @ace_data = (
'abc-123',
'abc-def-345.',
'abc-ghi-.678',
);
my $GET_NUMBER =
'\A # Start of record
(?:[a-z]{3}-){1,2} # hypen separted groups of three
(\d+\.\d* | # d[ddd][][dddd]
\.?\d+) # []d[ddd]
\Z # End of record'
;

my @sorted_data =
map $_->[1],
sort {$a->[0] <=> $b->[0]}
map [(my $number) = /$GET_NUMBER/xms, $_],
@ace_data
;

foreach my $record (@sorted_data) {
print $record, "\n";
}



Results:

abc-ghi-.678
abc-123
abc-def-345.



For this example, the RE only needs the two lines related to numbers. I am not clear what your data really is like
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives