tags:

views:

80

answers:

4
$value = $list[1] ~ s/\D//g;

syntax error at try1.pl line 53, near "] ~"

Execution of try1.pl aborted due to compilation errors.

I am trying to extract the digits from the second element of @list, and store it into $value.

+1  A: 

maybe you wanted the =~ operator?

P.S. note that $value will not get assigned the resulting string (the string itself is changed in place). $value will get assigned the number of substitutions that were made

newacct
@newacct: Then how come it gets assigned in [this case](http://stackoverflow.com/questions/3577087/how-to-read-from-a-file-and-do-a-grep-in-perl/3577114#3577114)?
Lazer
@values = ($list[1] =~ /\d/g);
hlynur
@newacct: thanks!
Lazer
A: 

And wanted \digits not non-\Digits. And have a superfluous s/ubstitute operator where a match makes more sense.

if ($list[1] =~ /(\d+)/) {
    $value = $1;
}
msw
I was trying to remove all non digits.
Lazer
TIMTOWTDI. The Perl philosohpy.
Philip Potter
your match only works if the digits are contiguous. the search-and-replace will work on something like `$100,000` for example
plusplus
+5  A: 

You mean =~, not ~. ~ is a unary bitwise negation operator.

A couple of ways to do this:

($value) = $list[1] =~ /(\d+)/;

Both sets of parens are important; only if there are capturing parentheses does the match operation return actual content instead of just an indication of success, and then only in list context (provided by the list-assign operator ()=).

Or the common idiom of copy and then modify:

($value = $list[1]) =~ s/\D//;
ysth
thanks @ysth. this is so unlike C!
Lazer
Actually, it's a lot like C. The operator you were using doesn't do in C what you were trying to do either. :)
brian d foy
A: 

You said in a comment that are trying to get rid of non-digits. It looks like you are trying to preserve the old value and get the modified value in a new variable. The Perl idiom for that is:

 ( my $new = $old ) =~ s/\D//g;
brian d foy