Re: getting a number out of a string....




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

.



Relevant Pages

  • Re: ISAM error when trying to create tablelink
    ... Dim tDef As DAO.TableDef ... Doug Steele, Microsoft Access MVP ... Dim str As String ...
    (microsoft.public.access.modulesdaovba)
  • Re: Using the Dictionary object
    ... Private Sub LV_ItemCheck ... Dim Str As String ... MyNext = MyNext - 1 ...
    (microsoft.public.excel.programming)
  • Re: str() should convert ANY object to a string without EXCEPTIONS !
    ... For strings, ... 'ascii' codec can't encode character u'\ue863' in ... And it is correct to fail, ASCII is only defined within range, ... If that str() function has returned anything but error on this, ...
    (comp.lang.python)
  • Re: input a string in gcc
    ... Basically what my code is supposed to do is accept a string from user ... fgets (str, sizeof str, stdin); ... Think of pointers like of checks - if they aren't backed by ... i-th element of arr to point to the j-th char in str. ...
    (comp.lang.c)
  • Re: ISAM error when trying to create tablelink
    ... that will work to change an existing similar tabledef.connect string. ... Doug Steele, Microsoft Access MVP ... Dim rs As New ADODB.Recordset ... Dim str As String ...
    (microsoft.public.access.modulesdaovba)