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

finding the last occurence of substring in string

by gri6507 (Deacon)
on Jul 26, 2012 at 15:43 UTC ( [id://983859]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow monks

I have a scalar which contains a stream of ASCII representation of binary data (one nibble = one ASCII char). If there are any bytes that are not defined, the ASCII representation of those bytes within the stream is '??'. There can be gaps within this stream and in general, I do not know the size of this stream up front. I need to get the byte address of the last defined byte within this stream. In the following example, that would be 4. While I can do something C-like, it just seems to me that there should be a much more Perl-ish way to do the same. Any ideas?

use warnings; use strict; # remember that a byte consists of 2 nibbles and each nibble can be re +presented as an ASCII character within the stream. my $stream = '0123??6789??????'; my $lastValidByteIndex; for my $byteIndex (0 .. length($stream)/2-1) { my $byte = substr($stream, $byteIndex*2, 2); $lastValidByteIndex = $byteIndex unless $byte eq '??'; } if (defined($lastValidByteIndex)) { print "last valid byte is at index $lastValidByteIndex\n"; } else { print "There is no valid data in the stream\n"; }

Replies are listed 'Best First'.
Re: finding the last occurence of substring in string
by BrowserUk (Patriarch) on Jul 26, 2012 at 16:03 UTC

    The position of the (first of) last 2 non-? bytes that are followed exclusively by ?s to the end of the string:

    $s =~ m[([^?]{2})[?]+$] and print $-[0];; 8

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thanks, the match operator gets me what I was looking for. But your post brings up another question. What is the meaning of $-[0] ?
        What is the meaning of $-[0] ?

        See the definition of @LAST_MATCH_START in perlvar.

        $-[0] is the value of the first element of @-. Which, after a successful match, contains the position in the string at which the capturing parens matched.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: finding the last occurence of substring in string
by johngg (Canon) on Jul 26, 2012 at 16:15 UTC

    I'm not sure I'm understanding correctly what you want to do but this gives the answer you seem to want.

    $ perl -Mstrict -Mwarnings -E ' > my $last; > my $pos; > my $stream = q{0123??6789??????}; > map { $last = $pos unless m{\?\?}; $pos ++ } > unpack q{(a2)*}, $stream; > say $last;' 4 $

    I hope this is helpful but perhaps you could clarify your requirement if I have misunderstood.

    Cheers,

    JohnGG

Re: finding the last occurence of substring in string
by Ratazong (Monsignor) on Jul 26, 2012 at 16:37 UTC

    Just for the sake of TIMTOWTDI (or fun, or for following Matthew 20:16), you may apply the following algorithm:

    • get the index of the reversed substring in the reversed string
    • calculate the position (lengthOfString minus index minus lengthOfSubstring)(with a possible off-by-one-correction)

    Rata

      if you want to get the reverse index why not just use rindex ? :)

Re: finding the last occurence of substring in string
by daxim (Curate) on Jul 26, 2012 at 16:15 UTC
    edit: this was wrong

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-18 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found