CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: $1 becomes undefined after reassignment / substitution: Edit Log



Zhris
Enthusiast

Jun 28, 2014, 11:14 PM


Views: 9427
$1 becomes undefined after reassignment / substitution

Hey,

I'm getting odd results when working with a re-assigned capture variable. I know of workarounds, but would like to understand the issue. It appears that as soon as I perform a substitution on the new variable, $1 "goes out of scope" (undefined). I've not used warnings in the example below in order not to muddy the output with "use of uninitialized value in concatenation" messages.


Code
my $str = "a  b c   d"; 

while ( $str =~ /(\w\s*)/g )
{
my $char = $1;
$char =~ s/\s+//g;
print ">$char, $1<\n";
}


Output:

Code
>a, < 
>b, <
>c, <
>d, d<


Expected output:

Code
>a, a  < 
>b, b <
>c, c <
>d, d<


Regards,

Chris


(This post was edited by Zhris on Jun 28, 2014, 11:18 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 28, 2014, 11:15 PM
Post edited by Zhris (Enthusiast) on Jun 28, 2014, 11:17 PM
Post edited by Zhris (Enthusiast) on Jun 28, 2014, 11:18 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives