newbie also

N

NEAL ZIERKE

I'm just learning perl, so that said.

question is.


If I have file names in directory like /mydir/abcd12e.fgh ,,, in unix system

I wish to test the "12" part of filenames. I understand and lot of examples
on net for directory stuff ( File::Find::name )

I wish to test against other file names that will look the same but the 12
would be different ( this is the version of the file ) , say it might look
like this /mydir/abcd45e.fgh, where as this is newest version.

What is the easiest way to test those 2 char's of file name in perl




So like




use File::Find;

find(\&myprocessing,"startdir");




sub myprocessing

{

if ( "test the "12" vs the "45" )

{

"move the new to old file code "

}




}
 
T

Ted Zlatanov

NZ> If I have file names in directory like /mydir/abcd12e.fgh ,,, in unix system

NZ> I wish to test the "12" part of filenames. I understand and lot of examples
NZ> on net for directory stuff ( File::Find::name )

NZ> I wish to test against other file names that will look the same but the 12
NZ> would be different ( this is the version of the file ) , say it might look
NZ> like this /mydir/abcd45e.fgh, where as this is newest version.

NZ> What is the easiest way to test those 2 char's of file name in perl

Logically, you want to group a bunch of files by their base name without
digits.

use File::Basename;

my %bybase;

sub store_name
{
my $name = shift @_;
my $key = basename $name;
$key =~ s/\d+//g;
push @{$bybase{$key}}, $name;
}

After you use store_name on every file name, the %bybase hash will
contain the groupings you want. Use Data::Dumper to see how it looks.

So unlike your example, you wouldn't check when you find the file, you'd
just store_name on that file name. Then after the full search is done
you would go through %bybase.

Ted
 
D

Dr.Ruud

Ted Zlatanov schreef:
my $key = basename $name;
$key =~ s/\d+//g;

I would probably make that

$key =~ s/[0-9]+/#/g;

or just

$key =~ s/[0-9]+/9/g;

to prevent collissions.
 
J

Jim Gibson

NEAL ZIERKE <[email protected]> said:
I'm just learning perl, so that said.

question is.


If I have file names in directory like /mydir/abcd12e.fgh ,,, in unix system

I wish to test the "12" part of filenames. I understand and lot of examples
on net for directory stuff ( File::Find::name )

I wish to test against other file names that will look the same but the 12
would be different ( this is the version of the file ) , say it might look
like this /mydir/abcd45e.fgh, where as this is newest version.

What is the easiest way to test those 2 char's of file name in perl

What are you trying to test? You can extract the digits using a regular
expression:

$file = '/mydir/abcd12e.fgh';
if( $file =~ /^(.+)(\d+)(.+)$/ ) {
my $ver = $2;
print "Version $ver of file $1..$3\n";
}

use File::Find;
find(\&myprocessing,"startdir");
sub myprocessing
{
if ( "test the "12" vs the "45" )
{
"move the new to old file code "
}
}

find will only have one file name at a time. You can save the file
names in a global array and process the contents of the array after you
have called find. For efficiency, extract the version numbers and put
the remainder of the file name in a hash as you traverse the array,
which will allow you to easily check if two versions of the same file
exist in the directory (untested):

my @files;
find( sub( push(@files,$File::Find::name) ), $startdir);
my %versions;
for my $file ( @files ) {
if( $file =~ /^(.+)(\d+)(.+)$/ ) {
my $name = "$1$3";
my $ver = $2;
if( exists $versions{$name} ) {
print "Versions $ver and $versions{$name} exist for $name\n";
# do whatever processing needed
}else{
$versions{$name} = $ver;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top