How do you match 25 filenames?

Discussion in 'Perl Misc' started by rnhurt@gmail.com, May 11, 2005.

  1. Guest

    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
    , May 11, 2005
    #1
    1. Advertising

  2. wrote in news:1115819507.500038.46790
    @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. Sinan Unur, May 11, 2005
    #2
    1. Advertising

  3. Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > wrote in news:1115819507.500038.46790
    > @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.


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

    Anno
    Anno Siegel, May 11, 2005
    #3
  4. <> wrote:

    > 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?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 11, 2005
    #4
  5. Joe Smith Guest

    wrote:

    > 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
    Joe Smith, May 11, 2005
    #5
  6. Guest

    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
    , May 18, 2005
    #6
  7. wrote in news:1116374093.614072.215290
    @g44g2000cwa.googlegroups.com:

    > Sinan,


    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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, May 18, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    636
  2. B.J.
    Replies:
    4
    Views:
    740
    Toby Inkster
    Apr 23, 2005
  3. Victor
    Replies:
    2
    Views:
    640
    Victor
    May 17, 2004
  4. jimgardener

    finding an exact match of filenames

    jimgardener, Oct 16, 2010, in forum: Python
    Replies:
    1
    Views:
    271
    Peter Otten
    Oct 16, 2010
  5. Alythh

    strange filenames pattern match

    Alythh, Jan 27, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    74
    Walter Roberson
    Jan 27, 2004
Loading...

Share This Page