Index and Regexs 
Author Message
 Index and Regexs

I want to find the position of the first number so, WHY can't I do this:

        $str = "WRITE (LU6,'(7H CLOCK ,12(9HCODE AMB.,A1:))') (' ',I=1,J)";

        $pos = index($str, /\d+/, 0);

        print "pos = $pos\n";

pos = 0

The same goes for

        $pos = index($str, /CLOCK/, 0)

pos = 0

Although this should be written as

        $pos = index($str, "CLOCK", 0)

pos = 25

I don't understand what is really going on here. How is the index
routine interpreting my regex?? How is this result different than
-1 (not found)??

How would you write it??



Thu, 25 Dec 1997 03:00:00 GMT  
 Index and Regexs

Quote:

>I want to find the position of the first number so, WHY can't I do this:

>    $str = "WRITE (LU6,'(7H CLOCK ,12(9HCODE AMB.,A1:))') (' ',I=1,J)";

>    $pos = index($str, /\d+/, 0);

>    print "pos = $pos\n";

>pos = 0

That's because index takes a string and a substring as the first 2
arguments, /\d+/ is a pattern which will try and match with the contents
of $_ and returns 1 or '' in a scalar context in perl 4.xxx depending on
whether the match succeeded or not, do the result of the $pos = index ...
will depend on the contents of $_.

One way to write it might be:

  $pos = $str =~ /\d+/ ? length ($`) : -1;

Quote:
>Although this should be written as

>    $pos = index($str, "CLOCK", 0)

>pos = 25

>I don't understand what is really going on here. How is the index
>routine interpreting my regex?? How is this result different than
>-1 (not found)??

That's because the regex is being applied to $_ and the scalar result of
the match is turned into the second argument to index so you end up doing
something like

  $pos = index ($str, undef, 0);

or

  $pos = index ($str, '1', 0);

The only odd thing is that in perl 4.036 index ("foo", "", 0) yeilds -1,
but print index ("foo", undef, 0) yeilds 0 which might not be the
behaviour you expect.

Hope this helps,

Mike
--
Mike Stok                        | The "`Stok' disclaimers" apply.


http://www.{*filter*}com.net/~stok/   | The inevitable WWW page (?)



Fri, 26 Dec 1997 03:00:00 GMT  
 Index and Regexs

Quote:
>I want to find the position of the first number so, WHY can't I do this:
>    $str = "WRITE (LU6,'(7H CLOCK ,12(9HCODE AMB.,A1:))') (' ',I=1,J)";
>    $pos = index($str, /\d+/, 0);
>    print "pos = $pos\n";
>pos = 0
>The same goes for
>    $pos = index($str, /CLOCK/, 0)
>pos = 0
>Although this should be written as
>    $pos = index($str, "CLOCK", 0)
>pos = 25
>I don't understand what is really going on here. How is the index
>routine interpreting my regex?? How is this result different than
>-1 (not found)??

The point you are missing is that the second argument to index is a
string, not a regex.  What happens, is that perl matches your pattern
against $_.  It happens to not match, so the // subexpression returns
"" and pos then goes on to find an occurrance of "" at position 0.

What you want, is:

        $str = "WRITE (LU6,'(7H CLOCK ,12(9HCODE AMB.,A1:))') (' ',I=1,J)";

        $str =~ /\d+/;
        $pos = length $`;

        print "pos = $pos\n";

pos = 9

Incidentally, if you're only interested in the position of the first
digit, it would be more efficient to use the pattern /\d/.

-
Hope this helps,

HansM



Sat, 27 Dec 1997 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Issues with interpolating regexs

2. telnet chars and chat regexs

3. maps and regexs

4. Matching many similar regexs

5. RegExs and question marks...a possible bug?

6. Compiling regexs for efficency

7. difference in execution time of regexs

8. use @records[$index] or $records[$index] ??

9. Searching an integer-indexed array & returning index of found

10. Indexing a Hash with a value returned from indexing an array...perl v 5.005_02 error?

11. Last valid index with array references

12. Version numbers in cpan index databases inconsistent with Date::ICal

 

 
Powered by phpBB® Forum Software