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

Ok, I'm trying to match a zip code. I need a false value returned if the zip code doesn't match, 1 returned if the 1st 5 digits match, and a 2 if all 9 digits match. Even, -1, 0, and 1 would be fine, I just need to be able to distinquish, what was matched. I was trying
my ($zip_match) = $card_zip =~ /(12345)\-?(6789)?/;
Which returns undef if there is no match but 1 if there is a 5 or 9 digit match. I tried
my (@zip_match) = $card_zip =~ /(12345)\-?(6789)?/g;
and @zip_match was populated correctly ['12345', undef] for a 5 digit match ['12345', '6789'] for a 9 digit match so why doesn't it return the number of elements in scalar context. I've also tried stuff like this, but to no avail:
my ($zip_match) = scalar($card_zip =~ /(12345)\-?(6789)?/);
I've figured out ways around this, but it is bothering me that I can't get this to work.

Thanks
--Tom

Replies are listed 'Best First'.
Re: Zipcode Regex Help
by dws (Chancellor) on Jun 25, 2003 at 21:26 UTC
    I need a false value returned if the zip code doesn't match, 1 returned if the 1st 5 digits match, and a 2 if all 9 digits match.

    How about something like this?

    sub zipMatch { my $zip_card = shift; return 0 unless $zip_card =~ m/\b(\d{5})(?:-(\d{4}))?/b/; return 1 unless $2; return 2; }
    The \b are to avoid accidentally matching an invalid zipcode like
    0000000000-0000000000
      I have something similiar to your code that is working now. I'm just curious, why the regex I was using isn't working the way the I expected.
        Because that's not how the return value of matches is defined. In scalar context, a succesful match returns the number of times it matched. Without the /g, this will always be 1. With /g, this might be more, but that wouldn't help in your case. An unsuccesful match in scalar context returns the empty string, regardless of any flags.

        That's the way how it works, and if you want to have different values depending what matched, you need to do a little more work.

        Abigail

Re: Zipcode Regex Help
by Abigail-II (Bishop) on Jun 26, 2003 at 01:33 UTC
    #!/usr/bin/perl use strict; use warnings; use Regexp::Common; while (<DATA>) { chomp; my $zip_match = 0; if (/$RE{zip}{US}{-keep}/) { $zip_match ++; $zip_match ++ if $6; } printf "Value %d for zip code '%s'\n", $zip_match, $_; } __DATA__ 6789 12345 12345-6789 Value 0 for zip code '6789' Value 1 for zip code '12345' Value 2 for zip code '12345-6789'

    Abigail

Re: Zipcode Regex Help
by PodMaster (Abbot) on Jun 25, 2003 at 21:29 UTC
    This is why
    $_ = 'in scalar context' x 3; print $_,$/; while(/in scalar context/g){ print $a++,$/; }
    "my( $scalar ) = EXPR" is list context (you should definetly read "List" is a Four-Letter Word).

    Try my $foo = () = /in scalar context/g; You should get 3.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I tried
      my $zip_match = () = $card_zip =~ /(12345)\-?(6789)?/g;
      and
      $_ = $card_zip; my $zip_match = () = /(12345)\-?(6789)?/g;
      Both Returned 0 on no match and 2 otherwise, even if only the first 5 digits match.
        What did you expect it to return? Consider this
        my $bar = 'asdf asdf asdf-rdf'; my $mor = () = $bar =~ /asdf(?:-rdf)?/; my $gor = () = $bar =~ /asdf(?:-rdf)?/g; my @gor = $bar =~ /asdf(?:-rdf)?/g; warn scalar @gor; die "$mor and $gor = @gor"; __END__ 3 at - line 6. 1 and 3 = asdf asdf asdf-rdf at - line 7.
        update:
        It returns the number of times the entire pattern matched ( aka the number of total matches). .
        My apologies. What's returned are the values of the capture buffers (parens create a capture buffer). my $mor = () = $bar =~ /asdf(?:-rdf)?/; could be rewritten as my $gor = my @gor = $bar =~ /asdf(?:-rdf)?/g;

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.