Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Remembering values from multiple condition 'if'

by TheHobbit (Pilgrim)
on Jul 01, 2002 at 21:40 UTC ( [id://178694]=note: print w/replies, xml ) Need Help??


in reply to Remembering values from multiple condition 'if'

Hi,
besides the solution proposed by other monks, there is the following

use strict; my @files = grep ($_,map -e $_ && $_ , qw(foo foobar bar)); if (@files) { print "there are files\n"; #here @files holds the names of the files print(join(",",@files),"\n"); } else { print "no files\n"; }
which places the existing file's names in an array.

cheers


Leo TheHobbit

Replies are listed 'Best First'.
Re^2: Remembering values from multiple condition 'if'
by Aristotle (Chancellor) on Jul 02, 2002 at 01:51 UTC
    Argh, I'm out of votes. Consider yourself ++ed. Just a question though, why don't you just do like so? my @files = grep -e, qw(foo foobar bar); ____________
    Makeshifts last the longest.

      Hi,
      Well, with that grep, @files will end up containing only ones, and you'll not even be able to sort out exactly 'which' files where there...

      Cheers


      Leo TheHobbit
        Actually, no - that would happen if I were using map. grep doesn't return the value of the expression, it returns the original element if the expression evaluates to true. Observe:
        $ touch a b e ; perl -le'@f=grep -e, qw(a b c d e f); print "@f"' a b e
        ____________
        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-20 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found