in reply to changing a string to a numeric variable

The following splits the line into words, then kills any embedded commas.

$_ = 'word 15,000 word word 20,000'; my @a = grep {s/(?<=\d),(?=\d)//g || $_} split;

Replies are listed 'Best First'.
Re: Re: changing a string to a numeric variable
by kappa (Chaplain) on Mar 18, 2004 at 22:39 UTC
    Why do you use grep here?
    $_ = 'word 0 word'; my @a = grep {s/(?<=\d),(?=\d)//g || $_} split;
    ...will of course lose data.
    $_ = 'word 0 word0,0word'; my @a = map {s/(?<=\d),(?=\d)//g; $_} split;
    ...will be better (as if there can be one wrong way better of another wrong way), but lose data too, btw.

      Tell me how it would "of course" lose data? It does work.

        web07:~[0]% perl $_ = 'word 0 word'; my @a = grep {s/(?<=\d),(?=\d)//g || $_} split; print join ':', @a, "\n"; ^D word:word: web07:~[0]%
        That '0' vanished because the grep-condition (s/...//g || $_) is false and grep is a filter.