Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: List manipulation headache

by elef (Friar)
on Feb 13, 2011 at 13:37 UTC ( #887866=note: print w/ replies, xml ) Need Help??


in reply to List manipulation headache

Depending on your input data, this should be pretty easy to do with a regex.
For instance, if your input file looks exactly like your snippet above, you could probably get away with just putting these two lines in a while loop:

s/ wap/, /g; s/, / wap/; # change back the first occurrence on each line


Comment on Re: List manipulation headache
Download Code
Re^2: List manipulation headache
by atancasis (Sexton) on Feb 13, 2011 at 15:30 UTC
    Interesting! Smart solution. Building on the excellent proposal by @elef, the expression can be further compressed to the following:
    s/ wap([2-9])/,$1/g
      There are a couple of ways in which that could break.
      It will break on lines where the first "wap" is not wap1 (we don't know if that can occur in the file) and it will break on wap11 through wap19, which seem to be valid data (OP: the rest can be abbreviated to 2,3,4,5,12,34).
      I seem to remember some regex element that tells perl to do a replacement on the nth occurrence(s) only, which could be used here, but I can't think of it now. Maybe I just made it up.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-07-23 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (152 votes), past polls