http://www.perlmonks.org?node_id=606601


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.

Replies are listed 'Best First'.
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.

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