×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Regexp help -- matching filenames with different versions

Regexp help -- matching filenames with different versions

Regexp help -- matching filenames with different versions

(OP)
I'm trying to loop through a group of files for processing, but some files may have different versions in the batch and I only need to process the latest version of each one.
I've got an array of filenames (reverse sort):
@stl_files = [
'STL-20120925.01.006_IPSP.CSV',
'STL-20120925.01.006.CSV',
'STL-20120925.01.001_IPSP.CSV',
'STL-20120925.01.001.CSV',
'STL-20120914.01.006.CSV',
'STL-20120914.01.001.CSV'
]

The '001' and '006' in the examples are the version numbers. So as I loop through these files, I need to match against previously checked processed files.

So when the 3rd file comes up, it needs to recognize that it's an earlier version of the 1st file, the 4th is an earler version of the 2nd, and the 6th is an earlier version of the 5th.

I'm thinking as each file is looked at, I'll add it to an array of viewed files. Then there should be some way to use a regexp to to bump the current filename against the viewed filenames, but I'm at a loss of how to do it effeciently. Can someone point me in the right direction?

Thanks,

_________
RottPaws

If you don't report a problem, I probably won't fix it.

RE: Regexp help -- matching filenames with different versions

Try this:

CODE --> Perl

#!/usr/bin/perl -w
use strict;

my %seen;

my @stl_files = (
        'STL-20120925.01.006_IPSP.CSV',
        'STL-20120925.01.006.CSV',
        'STL-20120925.01.001_IPSP.CSV',
        'STL-20120925.01.001.CSV',
        'STL-20120914.01.006.CSV',
        'STL-20120914.01.001.CSV'
);

foreach my $f (@stl_files) {
        # copy the filename
        my $s = $f;
        # strip out the version number (dd.ddd following an 8-digit date)
        $s =~ s/([0-9]{8}\.)[0-9][0-9]\.[0-9]{3}/$1/;
        if (defined $seen{$s}) {
                print "already processed a version of $f\n";
        } else {
                print "processing $f\n";
                #
                # insert processing code here
                #
                $seen{$s}=1;
        }
} 

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Regexp help -- matching filenames with different versions

I think the most efficient way is to build a hash where the key is the filename with the revision number replaced by some constant (or null) value and the value is the revision number. Note also that if the filenames have all the same structure, then it is more efficient to use string manipulation instead of regexp's. At the end you transfer the restored filenames into an array.

CODE -->

@stl_files=(
  'STL-20120925.01.006_IPSP.CSV',
  'STL-20120925.01.006.CSV',
  'STL-20120925.01.001_IPSP.CSV',
  'STL-20120925.01.001.CSV',
  'STL-20120914.01.006.CSV',
  'STL-20120914.01.001.CSV'
);
for(@stl_files){
  $rev=substr($_,16,3,'');
  if(exists$seen{$_}){
    $seen{$_}=$rev if $rev gt $seen{$_}; 
  }else{
    $seen{$_}=$rev;
  }
}
for(keys %seen){
  substr($_,16,0,$seen{$_});
  push @recent_files,$_;
}
print"@recent_files\n"; 

Franco
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Regexp help -- matching filenames with different versions

(OP)
Excellent! Thank you so much!!!

_________
RottPaws

If you don't report a problem, I probably won't fix it.

RE: Regexp help -- matching filenames with different versions

Is there a free software easy to use to test regex?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close