Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
P is for Practical
 
PerlMonks  

checking substring for numbers

by dwatson06 (Friar)
on Aug 01, 2002 at 16:00 UTC ( [id://186912]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I am trying to search data from a file for a numeric value. If there is not one, default to four zeros.
I am trying this...
if(substr($AResp, 8, 4) =~ eq /\d\d\d\d/) {
my $shopID = substr($AResp, 8, 4);
}else{
my $shopID = "0000";
}
What am I doing wrong?
Thanks,
Daniel

Replies are listed 'Best First'.
Re: checking substring for numbers
by dwatson06 (Friar) on Aug 01, 2002 at 16:06 UTC
    Please disregard the 'eq' in the source. I was trying to do a string comparison for empty input.

    Daniel
Re: checking substring for numbers
by bedk (Pilgrim) on Aug 01, 2002 at 16:10 UTC
    You've got a scoping problem, I think. 'my' gives you a variable local to a block, so as soon as you hit the right curly brace your $shopID goes away.

    Brian
Re: checking substring for numbers
by BorgCopyeditor (Friar) on Aug 01, 2002 at 16:11 UTC

    From perlsub:

    Unlike dynamic variables created by the local operator, lexical variables declared with my are totally hidden from the outside world

    BCE
    --Your punctuation skills are insufficient!

Re: checking substring for numbers
by BrowserUk (Patriarch) on Aug 01, 2002 at 18:11 UTC

    Now the guys have explained why your code is failing, you might also consider using:

    my $shop = m/.{8}(\d{4})/ ? $1 : '0000';

    or if you have an aversion to the ternary operator

    my $shop = '0000'; $shop = $1 if $AResp =~ /.{8}(\d{4})/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186912]
Approved by silent11
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.