Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Stupid mistakes I repeatedly make

by Tanktalus (Canon)
on Mar 28, 2005 at 15:44 UTC ( #442840=note: print w/ replies, xml ) Need Help??


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

Just to follow this thread off-topic ... how often does it matter? I can't even think of any time where I cared how many items were replaced with s///. And it's almost never when I don't care about the resultant string. So, yes, I'd agree that PHP's method is as flexible as Perl's, but I would suggest that the s operator is not Huffman-coded properly. The thing that we do most often (care about the resultant string) is the most work. Even if the difference is minor (an extra set of parens), Larry seems to talk a lot about "natural language" for perl 6, and I'd suggest that the current syntax is less natural. If, for example, s/// returned the resultant string in scalar context and the list of substitutions in list context, you could get the number of substitutions with my $num = @{[s/$re/foo/g]}; (although that syntax is probably not so ugly in perl 6 either). Given the rareness of its use (if my experience is any indication, or even in the same ballpark as reality in general), I think this would make more sense.

I seem to recall that perl6 is supposed to solve all of this, so not only is this thread off-topic, but probably moot anyway ;-)


Comment on Re^3: Stupid mistakes I repeatedly make
Download Code
Re^4: Stupid mistakes I repeatedly make
by itub (Priest) on Mar 28, 2005 at 16:29 UTC
    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.

      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).

Re^4: Stupid mistakes I repeatedly make
by Roy Johnson (Monsignor) on Mar 28, 2005 at 16:29 UTC
    You likely don't care how many times the replacement happened, but it's not uncommon to care about whether any replacements happened.
    if (s/foo/bar/) { ... } else { ... }
    (How many isn't even an issue unless you're using /g. It's just 1 or 0 — it replaced something or it didn't.)

    Caution: Contents may have been coded under pressure.
Re^4: Stupid mistakes I repeatedly make
by Ven'Tatsu (Deacon) on Mar 28, 2005 at 17:07 UTC
    I've use this in my code before:
    #actual data came over the network with inconstant line ends. my $text = "some\r\nmulti-line\ntext"; my $line_count = $text =~ s/\r?\n/\n/g;
    There were cenrtanly a dozen other ways to get the line count, but for what was being done this was the most strait forward, without doing double work.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-10-21 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (96 votes), past polls