How do you match 25 filenames?

R

rnhurt

Hello,

I have a project where I need to assign two scalars ($FileType &
$FileName) depending on the name of a particular file. I am reading in
a list of files (ls -l > file.in) and need to update a database every
time I see a particular filename pattern. The problem is, there are 25
different filename patterns, something like this:

Filename Pattern $FileType $FileName
=========================== ============ =============
xxxxxxC.*.dvr.ddhhmm SLS dvr
xxxxxxC.*.dio.ddhhmm SLS dio
xxxxC.emplchg.xfg.dddmmhh HR emplchg
xxxxxxC.rmd*.din.ddhhmm INV din
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
I have a project where I need to assign two scalars ($FileType &
$FileName) depending on the name of a particular file. I am reading in
a list of files (ls -l > file.in) and need to update a database every
time I see a particular filename pattern. The problem is, there are 25
different filename patterns, something like this:

Filename Pattern $FileType $FileName
=========================== ============ =============
xxxxxxC.*.dvr.ddhhmm SLS dvr
xxxxxxC.*.dio.ddhhmm SLS dio
xxxxC.emplchg.xfg.dddmmhh HR emplchg
xxxxxxC.rmd*.din.ddhhmm INV din
.
.
.

You have omitted crucial information.

What determines file type?

I have a feeling that has something to do with the characters you chose to
replace with x's.

There is no need for using 25 different patterns here.

In fact, if my guess is correct, and xxxxx's do correspond to file types,
then a simple split on the dot would give you everything you want.

If this does not help, then please read the posting guidelines for this
group to learn how you can help others help you.

Sinan
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:


You have omitted crucial information.

What determines file type?

I have a feeling that has something to do with the characters you chose to
replace with x's.

There is no need for using 25 different patterns here.

In fact, if my guess is correct, and xxxxx's do correspond to file types,
then a simple split on the dot would give you everything you want.

If this does not help, then please read the posting guidelines for this
group to learn how you can help others help you.

Plus the FAQ: perldoc -q "many regular expressions".

Anno
 
T

Tad McClellan

I am reading in
a list of files (ls -l > file.in)


There is no need to shell out for a list of files, you can
do that in native Perl:

perldoc -f opendir
perldoc -f readdir
perldoc -f glob

The problem is, there are 25
different filename patterns,

Does anyone have a thoughts about the best way to
approch this?


Yes, that is why the answer to your Frequently Asked Question
is in the Perl FAQ.

You are expected to check the Perl FAQ *before* posting to the
Perl newsgroup you know.


perldoc -q match

How do I efficiently match many regular expressions at once?
 
J

Joe Smith

Filename Pattern $FileType $FileName
=========================== ============ =============
xxxxxxC.*.dvr.ddhhmm SLS dvr
xxxxxxC.*.dio.ddhhmm SLS dio
xxxxC.emplchg.xfg.dddmmhh HR emplchg
xxxxxxC.rmd*.din.ddhhmm INV din

my %types = (dvr => 'SLS', dio => 'SLS', xfg => 'HR', din => 'INV');
while (<>) {
chomp;
my ($xC,$foo,$bar,$date) = split /\./,$_;
next unless $xC =~ /C$/; # [Assuming this is a requirement]
warn "Date '$date' is not numeric for $_\n" unless $date =~ /^\d+$/;

if (defined my $FileType = $types{$bar}) {
my $FileName = ($bar eq 'xfg') ? $foo : $bar;
print "For $_, FileType=$FileType and FileName=$FileName\n";
} else {
warn "Unrecognized file type '$foo' '$bar' for $_\n";
}
}

-Joe
 
R

rnhurt

Sinan,

The xxxxx's represent ever changing numbers (in this case location
ID's) and they are irrelevant to the type or name that I need to
associate with them.

Thank you all for the pointer to "matching many expressions" - I did
search quite extensively (both on Usenet and perldoc) but for whatever
reason didn't find what I needed. I will check again and hopefully
find the answer.

Thank you again for your time and patience with a Perl newbie. :)

Later...
Richard
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g44g2000cwa.googlegroups.com:

Please quote some context when replying. This is very important. I can't
access the original article via my ISP any more, and I am not in the
mood for a Google search.
The xxxxx's represent ever changing numbers (in this case location
ID's) and they are irrelevant to the type or name that I need to
associate with them.

I vaguely remember something about that, as well as the fact that you
had not explained what the exact correspondence between filenames and
types was.
Thank you all for the pointer to "matching many expressions" - I did
search quite extensively (both on Usenet and perldoc) but for whatever
reason didn't find what I needed.

Are you sure you know how to determine the type of a given file based on
its name? Forget about Perl, there must be a rule by which you could
deduce that just by looking at the name of the file.

Try to explain that, and a solution will likely present itself.

Then, post here to figure out how to improve it.

Sinan
 

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