Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

substr function

by tej (Scribe)
on Jan 13, 2011 at 14:49 UTC ( [id://882149]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

Where can I find code for "substr"? I actually want to modify it according to my requirements

Thank you

Replies are listed 'Best First'.
Re: substr function
by Corion (Patriarch) on Jan 13, 2011 at 14:53 UTC

    Search for pp_substr in pp.c.

    It's not a good idea to want to modify substr on that level. You can use subs (I think) and override substr with your own custom function. That makes it still a bad idea, but at least you can write your replacement in Perl that way.

    What problem are you trying to solve that involves replacing substr?

    Update: subs seems to be the wrong module, but I'm sure I remember a module that allowed one to replace built-ins. Maybe I'm just thinking of CORE::GLOBAL:: though:

    BEGIN{ *CORE::GLOBAL::substr=sub($$$) { die }; } print substr 'foo',2,3;

      I want to understand how it is working and then write my own subroutine for my script

      Suppose i have string that contains tags like "<bold>" it should not count this tag. If string has something like "<194>" I want substr to consider it as one character.

      Thank you very much

        Then write your own implementation of substr instead of replacing it. Most likely you'll need to write a parser for whatever language you're parsing. It shouldn't be hard, and "replace substr" clutters your problem with many hard (and weird) problems that are unrelated to the task.

        Write an ordinary Perl program that filters the text, then measures its length using the built-in substr function.

        You're trying to solve an everyday text processing problem in a very peculiar and unconventional way. Writing a custom substr function to measure a particular kind of "string" is like inventing a bathroom scale that knows when your hair is wet and you haven't removed your shoes, yet accurately measures your bone-dry, barefoot weight.

        I'm curious: Do you want to measure the lengths of the strings in bytes, in encoded characters (Unicode code points), or in real-world characters (Unicode extended grapheme clusters)?

        CLARIFICATION: I admit I sort of conflated substr and length in this post. My excuse is that I was fixated on the words "count" and "one character" in tej's restatement of his Y problem:

        Suppose i have string that contains tags like "<bold>" it should not count this tag. If string has something like "<194>" I want substr to consider it as one character.
Re: substr function
by ikegami (Patriarch) on Jan 13, 2011 at 16:51 UTC

    This is most definitely an XY problem. Sounds like you want to do something like the following, but you haven't really said what you are trying to do.

    # Tokenise my @tokens = $doc =~ /<[^>]*>|./sg; # Extract text my @text_tokens = grep /^<|<[0-9]+>/, @tokens; # Do stuff with @text_tokens # ... # Print resulting text print join('', @text_tokens);

    or maybe

    # Tokenise my @tokens = $doc =~ /<[^>]*>|./sg; # Extract text my $text = ''; for (@tokens) { if (/^<([0-9]+)>/) { $text .= chr($1); } elsif (/^[^<]/) { $text .= $1; } } # Do stuff with $text # ... # Print resulting text print join('', $text);

    Or do you need to output back in the original format (e.g. with bold tags in)?

    Update: Added second snippet.

Re: substr function
by dcd (Scribe) on Jan 14, 2011 at 07:49 UTC
    The others have given much better advice but if you download the sources for perl you can see the code you say you want to modify. Hopefully this will convince you that you should listen to the wiser PerlMonks.
    perl -ne 'print "$ARGV:$.:$_" if /substr/ .. /^}/' pp.c
    The above will extract and print the substr function that is in the perl source file pp.c.

      Or click "Read more..." below. :-)

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found