I have a directory with data files having longish, informative names; part of each file name is a unique numeric identifier (other parts are date_time, file type). I want to take a list of chosen numeric ID values, and look up the full path/file name that matches each item in the list.

So I use a file glob to match each ID value, but only half of the list elements come out as intended:

$ ls test bar_002.tst baz_003.tst faz_004.tst foo_001.tst $ cat test.list 001 002 003 004 $ perl -pale '$p=<test/*_$F[0].tst>; s/^/$p /' test.list test/foo_001.tst 001 002 test/baz_003.tst 003 004
I can't figure out why the glob only works on every other iteration, or how to make it work on every iteration. This perl 5.8.7, on both macosx and freebsd.

(The made-up example is simpler than the real case: the directory actually contains thousands of files, and I have a list of 200 ids to extract path/file strings for. I know there are other ways to do this, but I'm curious about the behavior of the file glob in this case.)

UPDATE: Nevermind -- I should have looked at perldoc -f glob first:

glob In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix she +ll /bin/csh would do. In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
Just changing the one-liner to start with '($p)=...' did the trick. Sorry for the spurious post.

In reply to Problem with one-liner using file globs by graff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.