Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Faced with Null/blank/empty value

by bh_perl (Monk)
on Jul 22, 2003 at 07:38 UTC ( [id://276642]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
This is my script to capture some info as below:

$$ref_entry{$1} = $2 if /^([\w ]+?)\s+:.(\d+)/; $$ref_entry{$1} = $2 if /^([\w ]+?)\s+:.'(.*)'/; $$ref_entry{$1} = $2 if /^([\w ]+?)\s+:.>(.*)</; print "TEST $1 ==> $2\n";
Info is here:
Location Identity : >0085 2f5b< 132321312 Required Bearer Capability : ><

I'm would be faced a problem when any items is null or blank or empty, as example on the "Required Bearer Capability" items as above. How could I faced it ?

Any ideas ?....

20030722 Edit by Corion: Removed PRE tags, added formatting

Replies are listed 'Best First'.
Re: Faced with Null/blank/empty value
by cLive ;-) (Prior) on Jul 22, 2003 at 08:35 UTC
    I may be wrong here, but it looks like you're only looking for one of these to match. If so, why not do this:
    /^([\w ]+?)\s+:.(\d+)/ or /^([\w ]+?)\s+:.'(.*)'/ or /^([\w ]+?)\s+:.>(.*)</ or /^([\w ]+?)(.*?)/ # no value or next; # I'm assuming this is in a loop somewher +e $$ref_entry{$1} = $2 || 'default value here'; print "TEST $1 ==> $2\n";

    .02

    cLive ;-)

      Good idea - especially if it is in a loop. Instead of all three regexes every time, it'll do a maximum of 3.

      In order to get the same results, however, the three regexes need to be reversed. In bh_perl's example, when the first match and the last match succeeded, $1 and $2 are set in the third match. In your example, if the first match succeeds the third is never evaluated so $1 and $2 are set by the first match. Reverse the three matches and the results will match the OP's.

        I assumed that only one match was required and that bh_perl was just a little inexperienced here in coding that idea. If you look, the 3 regular expressions are exclusive - ie, if one matches, the other two won't.

        cLive ;-)

Re: Faced with Null/blank/empty value
by TomDLux (Vicar) on Jul 22, 2003 at 07:56 UTC

    Use some default value. How about zero or the empty string or 'N/A'? You're the only one who knows what these values mean, so you'll have to figure out what an appropriate default value might be.

    You might use a default() routine:

    sub default { return 0 unless( @_ && length $_[0] ); return $_[0]; } $$ref_entry{$1} = default( $2 ) if /^(\w +?)\s+:.(\d+)/; $$ref_entry{$1} = default( $2 ) if /^(\w +?)\s+:.'(.*)'/; $$ref_entry{$1} = default( $2 ) if /^(\w +?)\s+:.>(.*)</

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found