Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

character location question

by EchoAngel (Pilgrim)
on Mar 11, 2005 at 15:22 UTC ( [id://438672]=perlquestion: print w/replies, xml ) Need Help??

EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Are there any functions that would return the location of the characters found first from left to right of a text? IE "hello world , kill bill" , so if i search for ll, i want to the location of ll from hello word then the next words kill then bill.

Replies are listed 'Best First'.
Re: character location question
by RazorbladeBidet (Friar) on Mar 11, 2005 at 15:36 UTC
    Try LAST_MATCH_END when using a regular expression.

    Update:

    Regex's are really unnecessary for this on second thought.

    How about:
    my $i = 0; while ( $i != -1 ) { $i = index( $text, "ll", $i ); if ( $i != -1 ) { print $i, "\n"; $i = $i + 1; } }
    or (equivalently)
    my $i = 0; print $i++, "\n" while ( ( $i = index( $text, "ll", $i )) != -1 );
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: character location question
by ZlR (Chaplain) on Mar 11, 2005 at 15:42 UTC
    Hmm, i think :
    my $s = "hello world, kill bill" ; print pos $s, " " while $s =~ /ll/g
    ... does almost what you want :)
    It returns : 4 17 22
    Maybe you need to substract the length of the string you locate ?

    Hope this helps,

    zlr

      thanks!
Re: character location question
by davidj (Priest) on Mar 11, 2005 at 15:43 UTC
    Take a look at String. It has some string functions including charAt and indexOf. That ought to help you.

    davidj
      And while you're at it, you should switch to Java!! </sarcasm>

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: character location question
by Joost (Canon) on Mar 11, 2005 at 15:37 UTC
    Sounds like homework, but here's a tip.
    my $i = index reverse($phrase),$what;
    updated: ()
    meh. that's not good ofcourse. I think this works ok:
    use strict; print join(",",rindexes("hello world , kill bill","ll")); sub rindexes { my $s = reverse shift; my $what = reverse shift; my @indexes; my $old = 0; while ((my $i = index $s,$what,$old) > -1) { push @indexes, length($s) - $i - length($what) +1; $old = $i+1; } @indexes; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438672]
Approved by pelagic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found