Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Simple or not

by ELISHEVA (Prior)
on Apr 06, 2009 at 15:09 UTC ( [id://755755]=note: print w/replies, xml ) Need Help??


in reply to Simple or not

Your regular expression: $line =~ s/ s{2,}/\t/s is converting " ss" and " ssss" to tabs and it is only converting the first one because you don't have a global flag at the end. To replace the tabs you would need s/\s\s+/\t/g or s/\s{2,}/\t/g. Note the backslash before the "s".

As for keeping the final newline, I would recommend stripping it before you apply the regex and adding it after you are done. There are ways to do that with regular expressions alone but your regular expression would be quite complicated. So the code to prep the line should look something like this:

while(my $line = <FH>) { #strip newline chomp $line; #replace runs of two or more spaces with tabs $line =~ s/\s\s+/\t/g; #add back newline $line .= "\n"; #.... do whatever with $line .... }

Best, beth

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found