Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Setting a Variable from filename (updated)

by AnomalousMonk (Archbishop)
on Apr 30, 2018 at 03:06 UTC ( [id://1213778]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Setting a Variable from filename
in thread Setting a Variable from filename

Yet Another Way to capture first matching file, if any, and number of matching files concisely:

c:\@Work\Perl\monks>perl -wMstrict -le "my @files = qw(xxx 1st_CF_xx CF__ 2nd_CF_yy CF); ;; my $n_CF_ = my ($first_CF_) = grep /_CF_/, @files; ;; print qq{number of _CF_ files: $n_CF_}; if ($n_CF_ == 1) { print qq{just one: '$first_CF_'}; } " number of _CF_ files: 2 c:\@Work\Perl\monks>perl -wMstrict -le "my @files = qw(xxx 1st_CF_xx CF__ CF); ;; my $n_CF_ = my ($first_CF_) = grep /_CF_/, @files; ;; print qq{number of _CF_ files: $n_CF_}; if ($n_CF_ == 1) { print qq{just one: '$first_CF_'}; } " number of _CF_ files: 1 just one: '1st_CF_xx' c:\@Work\Perl\monks>perl -wMstrict -le "my @files = qw(xxx yyy zzz); ;; my $n_CF_ = my ($first_CF_) = grep /_CF_/, @files; ;; print qq{number of _CF_ files: $n_CF_}; if ($n_CF_ == 1) { print qq{just one: '$first_CF_'}; } " number of _CF_ files: 0

Update: Be aware that if no files match the grep condition in the example code above,  $first_CF_ is undefined.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-25 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found