Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Stupid mistakes I repeatedly make

by itub (Priest)
on Mar 28, 2005 at 16:29 UTC ( [id://442846]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Stupid mistakes I repeatedly make
in thread Stupid mistakes I repeatedly make

I've often seen code that, although it doesn't care about the exact number of replacements, it cares about whether a replacement was made (the number would be 0 or 1 since it's not a global replacement). This is typically used for tokenizing by deleting the tokens from a string (note: this might not very efficient for long strings!). The following is a simple example for tokenizing a space-delimited list of numbers.
$s = "213 3218 213"; while ($s =~ s/^(\d+)\s*//) { print "$1\n"; } if (length $s) { print "Error! didn't expect this: '$s'\n"; }

This contrived example could probably be done more simply in other ways, but for more complicated tokenizers it is not such a bad approach.

Replies are listed 'Best First'.
Re^5: Stupid mistakes I repeatedly make
by Tanktalus (Canon) on Mar 28, 2005 at 16:40 UTC

    I stand corrected. Thanks, itub and Roy Johnson. Normally I do this in two phases (the match, then the substitution), but that's just me, and not something to advocate as the One True Perl Way. :-) I just find it makes more sense, but largely because I forget about s returning the number of matches, which is largely due to lack of need (usually I do the substitution and continue without any branching on whether it was succesful or not).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2025-07-12 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.