Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Howto strip 42 from comma separated list using s///

by jbware (Chaplain)
on Nov 03, 2005 at 13:23 UTC ( [id://505319]=note: print w/replies, xml ) Need Help??


in reply to Howto strip 42 from comma separated list using s///

Is this maybe the regex that you're looking for? There may be more elegant ways, but this seems alot easier to decipher.
s/(,42\b|\b42,|^42$)//g;
Update: Added the case for a single element of just "42" per Not_a_Number's and Perl Mouse's suggestions. Good catch; that's what a quick solution gets me, I didn't test all cases. I should note too, the position of 42 at the end of the list is important, so the regex engine doesn't try and grab that without looking for commas first.
Update: Perl Mouse makes a good point here: 505335 on boundary checking too, so I made another mod to the regex to take that into account as well.
Update: Ok, this is it; no more updates. One catch w/ boundary checking is in the odd case of "41,I like 42 things, 43", which would replace the "42", but shouldn't. I think in the context of this question this is really isn't an issue and \b works fine. To combat that though, I just hit what really was being addressed, "^42$", where its the only element.
Update: lol. I give up; one more. Perl Mouse pointed out partial number matches, so the boundary checking is back in a modified form. He has this below too. I only mod it here so in case someone takes a quick glance, they can get my best version (with the help of mightier monks than I) instead of my regex version from like 4 iterations ago.

-jbWare

Replies are listed 'Best First'.
Re^2: Howto strip 42 from comma separated list using s///
by Not_a_Number (Prior) on Nov 03, 2005 at 13:34 UTC
    s/(,42|42,)//g;

    The problem with this is that a list, by definition, might contain only one element. What if that element is 42?

Re^2: Howto strip 42 from comma separated list using s///
by Perl Mouse (Chaplain) on Nov 03, 2005 at 14:08 UTC
    s/(,42|42,|\b42\b)//g;
    Tricky, isn't? Unfortunally, you still haven't cover all cases correctly:
    $ perl -wle '$_ = "142,143"; s/(,42|42,|\b42\b)//g; print' 1143
    You need to anchor all cases:
    s/,42\b|\b42,|^42$//; # Or s/\b(?:,42|42,|42)\b//; # Or s/\b42,|,?42\b//;
    Perl --((8:>*
      Yeah, and painful. Although it can be done it regex, it just goes to show why I would much rather iterate through a list and process each element individually instead of handling all the element special cases in a regex. From a processing efficiency standpoint I'm not sure which is better, but I think regarding a programmer's efficiency, and future understanding, I'd lean toward looping through a list and handling each element on its own as a better decision. It just seems cleaner to me (as fun as regex exercises are); but maybe that's just me.

      -jbWare
Re^2: Howto strip 42 from comma separated list using s///
by Perl Mouse (Chaplain) on Nov 03, 2005 at 13:37 UTC
    That wouldn't work on a list containing just '42'. It will the string unmodified instead of ending up with the empty string.
    Perl --((8:>*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-18 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found