Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to get only files ending with a ,<word>

by perlNinny (Beadle)
on Jan 11, 2006 at 22:18 UTC ( #522578=perlquestion: print w/replies, xml ) Need Help??

perlNinny has asked for the wisdom of the Perl Monks concerning the following question:

How do I get only the files ending with a ,<word>

I thought this would work but I am my username:

my @versions; print "$dir\n"; opendir(DIR,$dir) || die "Can't open $dir: $!"; @versions = grep {/\b,.*?\b/ && -f "$dir/$_" } readdir(DIR); closedir DIR; print "@versions\n";

20060111 Janitored by Corion: Added formatting

Replies are listed 'Best First'.
Re: How to get only files ending with a ,<word>
by merlyn (Sage) on Jan 11, 2006 at 22:24 UTC
    The \b anchor matches between an alphanumeric character and a non-alphanumeric. If you have /\b,/, that means that the character on the "other side" of the \b (away from the comma) has to be an alphanumeric character. How many filenames do you have that have an alphanumeric character followed by a comma?

    Perhaps you wanted /,\w+$/ instead?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      How many filenames do you have that have an alphanumeric character followed by a comma?

      I get those with CVS.

Re: How to get only files ending with a ,<word>
by Paladin (Vicar) on Jan 11, 2006 at 22:24 UTC
    You'll have to be a bit more specific on what is not working. It seems to work just fine here:
    [~/tmp]$ touch foo,bar [~/tmp]$ touch baz [~/tmp]$ touch foo,baz [~/tmp]$ touch a b c [~/tmp]$ perl test.pl . foo,bar foo,baz
      Yes, you are right. What I want is this: I want the list to contain only the portion after the ,. So, using your example, I want to see bar bas and each a separate element in the list. THANKS for the help!
        Then something like:
        my @comma_extensions = map { /,(\w+)$/ ? $1 : () } @files;
        will produce what you want.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to get only files ending with a ,<word>
by bart (Canon) on Jan 11, 2006 at 22:27 UTC
    I think your regexp is too strict. Try changing it to /,\b/, I don't think there's much to match that you don't want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://522578]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-08-09 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?