Re: getting a number out of a string....
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 28 Dec 2005 18:17:15 -0800
Chris Devers wrote:
> On Wed, 28 Dec 2005, David Gilden wrote:
>
> > How does one just get the number part out of a string?
> >
> > The script below just prints 1.
>
> Right. All it's doing is reporting a successful, true, match.
>
> You need to fix where the parentheses are being used:
>
> > foreach $str (@str) {
> > $num = ($str =~ /\d+/);
> > print "$str : $num\n";
> > }
>
> foreach (@str) {
> ( $num = $_ ) =~ /\d+/;
> print "$_ : $num\n";
> }
>
> That should work better, and also avoids the confusing and unnecessary
> $str temporary scalar that looks too much like the $str[] array. Also,
> it's usefully indented, because Readability Matters.
>
Please run your code before posting it as a solution.
This does absolutely nothing the OP asked for. This assigns $num to be
the value of $_, and then runs a pattern match against that value. No
where is the \d+ parsed or stored. This code prints two copies of the
same value, separated by a space-colon-space.
Paul Lalli
.
- References:
- getting a number out of a string....
- From: David Gilden
- Re: getting a number out of a string....
- From: Chris Devers
- getting a number out of a string....
- Prev by Date: Re: Each char / letter --> array from string value
- Next by Date: getting a time diff from strings
- Previous by thread: Re: getting a number out of a string....
- Next by thread: Re: getting a number out of a string....
- Index(es):
Relevant Pages
|