in reply to Re: it seems i don't understand =~
in thread it seems i don't understand =~

thanks ikegami. i don't get the " won't compile comments"..

>type 754047.pl $value =~ s/\D//g; $value =~ $1 if( $value =~ /(\d+)/ ){ $value =~ $1 } else { undef $value; } >perl -c 754047.pl syntax error at 754047.pl line 3, near "){" 754047.pl had compilation errors.

the two lines fix a bug in the script which "seems" to be working ok on my host now..??

Seems is the operative word

my $value = "abc456def"; $_ = $value; $value = m/(\d+)/; print "$value\n"; # Prints 1, not 456

Fix:

my $value = "abc456def"; $_ = $value; ($value) = m/(\d+)/; print "$value\n"; # Prints 456

A more elegant version that doesn't clobber a global:

my $value = "abc456def"; ($value) = $value =~ m/(\d+)/; print "$value\n"; # Prints 456

Replies are listed 'Best First'.
Re^3: it seems i don't understand =~
by partymeeple (Novice) on Mar 30, 2009 at 09:14 UTC
    oh.. i get it.. i thought the not working code was referring to my 2 lines, not the other segment (i didn't bother to look at it as i wanted a shorter model).

    yes my code seemed to work. but actually was not. your suggestion fixed it perfectly.

    thanks again.

    beware the farmers.