in reply to Re: $1 don't reset (non loop)
in thread $1 don't reset (non loop)

If I see it right, you didn't get it quite correct. $addressX should become undef if no match. So this should work:
if ($adress1 =~ /^([A-Za-z0-9öäåÖÄÅ][A-Za-zöäåÖÄÅ0-9\s\-\.\,]*)$/) { $adress1 = $1; } else { $adress1 = undef; } # and so on...
which should be reducable to:
Which is not, as you'll see below, reducable to:
{ $adress1 =~ /^([A-Za-z0-9öäåÖÄÅ][A-Za-zöäåÖÄÅ0-9\s\-\.\,]*)$/) $adress1 = $1; } { $adress2 =~ /^([A-Za-z0-9öäåÖÄÅ][A-Za-zöäåÖÄÅ0-9\s\-\.\,]*)$/) $adress2 = $1; }
Update: Thanks to Abigail-II who found out my mistake.

Replies are listed 'Best First'.
Re: $1 don't reset (non loop)
by Abigail-II (Bishop) on Feb 17, 2004 at 11:56 UTC
    No, not really. That only works if $1 happens to be undefined. Which you can't count on. Watch:
    #!/usr/bin/perl use strict; use warnings; "meep" =~ /(.*)/; # Set $1. my $address1 = "Tsjakka"; # Will match. my $address2 = "Tsjakka!"; # Will not match. { $address1 =~ /^([A-Za-z0-9öäåÖÄÅ][A-Za-zöäåÖÄÅ0-9\s\-\.\,]*)$/; $address1 = $1; } { $address2 =~ /^([A-Za-z0-9öäåÖÄÅ][A-Za-zöäåÖÄÅ0-9\s\-\.\,]*)$/; $address2 = $1; } print $address1, "\n"; print $address2, "\n"; __END__ Tsjakka meep
    $address2 doesn't become undef. The solution is much simpler. The regex will either match the entire string, or there's no match. So, we could just do:
    /^([A-Za-z0-9öäåÖÄÅ][A-Za-zöäåÖÄÅ0-9\s\-\.\,]*)$/ or $_ = undef for $address1, $address2;

    Abigail