Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: A Case with 5 Var's

by dorward (Curate)
on Jan 25, 2007 at 10:42 UTC ( #596445=note: print w/ replies, xml ) Need Help??


in reply to A Case with 5 Var's

Use a hash instead of five scalars. Then you can do:

my @found; foreach my $field_name (qw/name vorname plz tel tel49/) { push(@found, $field_name) if ($my_hash{$field_name}); }

(Untested)

That will give you an array of all the fields that are set. This may or may not be helpful - you didn't say what you planned to do with the results.


Comment on Re: A Case with 5 Var's
Download Code
Replies are listed 'Best First'.
Re^2: A Case with 5 Var's
by Fletch (Chancellor) on Jan 25, 2007 at 13:36 UTC

    Or more succinctly:

    my @found = grep exists $my_hash{ $_ }, qw( name vorname plz tel tel49 + );

    Update: Good point below. Omit the exists if that's the desired behavior. MENTAL NOTE: NO POSTING OR UPDATING NODES BEFORE CAFFEINE TAKES EFFECT.

      The original code doesn't consider an empty string to be "found", but exists will.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2015-07-29 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls