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

Re: Stuck in komplexer regex, at least for me

by johngg (Abbot)
on Mar 26, 2007 at 15:49 UTC ( #606601=note: print w/ replies, xml ) Need Help??


in reply to Stuck in komplexer regex, at least for me

From your output it looks as if you want to replace the first occurrence of multiple zeros with a single zero, any subsequent multiple zero group should be left alone. This seems to work

#!/usr/local/bin/perl # use strict; use warnings; print map { s{0{2,}}{0}; $_ } <DATA>; __END__ 215000007801 300000324002 890000457651 210004563401 201045139158

and the output is

21507801 30324002 890457651 2104563401 201045139158

I hope this is of use.

Cheers,

JohnGG

Update: Looks like I've misunderstood. If you say the first zero should not be touched, why does 890000457651 become 89457651 and not 890457651? Perhaps you could clarify what you require.


Comment on Re: Stuck in komplexer regex, at least for me
Select or Download Code
Re^2: Stuck in komplexer regex, at least for me
by ultibuzz (Monk) on Mar 26, 2007 at 16:09 UTC

    sorry forgot to say that the rule first 0 no touch only applies when its after position 3
    kd ultibuzz

      I'm afraid things are becoming more confusing rather than less. From the first four examples in your desired output I can guess at these rules:

      Look for a sequence of two or more zeros

      If the sequence found starts in position 1, 2 or 3, (counting from 1 but note that Perl usually counts from 0), replace all the zeros by nothing

      If the sequence starts from position 4 or greater, replace all but one of the zeros with nothing

      I can't see how the fifth example fits into anything you have described.

      Cheers,

      JohnGG

Re^2: Stuck in komplexer regex, at least for me
by blazar (Canon) on Mar 27, 2007 at 09:22 UTC
    print map { s{0{2,}}{0}; $_ } <DATA>;

    I know this is completely OT and that in this particular case it wouldn't make a difference, but we recommend all the time against slurping files in all at once unless really needed, so please do not spread the word against this recommendation:

    s/0{2,}/0/, print while <DATA>;

    is not terribly more verbose. (I also changed the curlies as delimiters in the s operator because in this case they seemed confusing to me.)

      we recommend all the time against slurping files in all at once unless really needed

      Do we? How strange. Why?

      Seems to me that slurping is a perfectly valid technique in the right circumstances, for instance, parsing command output or working with small to middling data sets. People have even gone to the trouble of writing modules to support the idiom.

      In this case it would appear that ultibuzz has a data set of 5000000 numbers so slurping, as it turns out, would definitely not be appropriate.

      Cheers,

      JohnGG

        We recommend all the time against slurping files in all at once unless really needed

        Do we? How strange. Why?

        Seems to me that slurping is a perfectly valid technique in the right circumstances, for instance, parsing command output or working with small to middling data sets. People have even gone to the trouble of writing modules to support the idiom.

        I know that is a valid technique "in the right circumstances", that's why I didn't write "ever", but "unless really needed". Even when working with non-huge data sets it doesn't buy much after all. Nor is it really, strictly, required to "parse command output" (whatever that means - I suppose you mean the output of qx//.) The point still being that there are so many newbies here (possibly including the OP himself) and although I was the first one to specify that "in this particular case it wouldn't make a difference" (meaning precisely that the involved data set is tiny,) I'm afraid that they may see your post and think: gee, so this experienced Perl hacker uses

        print map { something } <$fh>;

        to iterate over the DATA filehandle, and get into the habit of doing so with all of their filehandles, which may bite them in the neck first or later, in which case we will have to tell them what's wrong with their code in another circumstance, when it may already be a rooted habit. OTOH

        print something while <$fh>;

        certainly doesn't do any harm with either small or huge data sets.

        To be clear, in Perl TMTOWTDI all the time, granted, but there are strongly preferred means to do particular tasks. In Perl 5 the "standard" way to read over a file for processing each line of it singularly (or in relation say to the nearest neighbouring ones, but let's not be too fussy, please!) is the while loop: it even has special syntactic sugar associated to it for this particular semantics. In Perl 6, with its lazy evaluation model, we will have for pretty much for everything, but that's a whole another story.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-08-29 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls