I'm a new user of this site and first time poster. Forgive me if I error ;-)
You said:
1. replace one or more spaces with a tabBut 1 says the reverse of what 2 says, if I'm reading it correctly.2. see #comment: in the line (-> $_ ) replace first tab with a space
So, trying to answer 1: s/\s+/\t/does what you want, once, on the first group of white space characters found.
And, again, trying to answer 2: s/\t/ /will do the trick, for the first tab found in the input, converting it to a space.
If you want these to work on all groups of whitespace or on all tabs found on input, add the 'g' modifier, as in your commented example:
s/\s+/\t/g s/\t/ /g
Following are some comments regarding whitespace, for those interested, and on the code snippet provided by star7.
Technically, whitespace includes any character that causes a print head to move without generating any marks on the paper. This includes: space, tab, newline, carriage return and vertical tab. The \s will match any of these items.
You mention using 'the option e', I presume you mean a modifier for the substitute. The 'e' modifier causes perl to do an 'eval' of the substitution result, which is to say execute some piece of code after substituting values into it. I don't think you want that. Your second line in the while loop has an 's' modifier, which also makes no sense, since: a) it tells perl to treat a newline just like any other character (ie not an end of line), but you've removed the only newline you'd find in the string based on the way you're reading the input; b) the 's' modifier affects the way the 'dot' special character works and you're not using a dot in your regex. You probably intended the 'g' modifier. But even that may not be needed, depending on whether the regex in the third line of code or the one in the comment, line 5, is what you want. Your commented example in line 5 only has a single 'whitespace' character, the tab, within the part of the string you seem to be interested in, and which is anchored to the beginning of the line, forcing operation on a particular part of the input string, only. It won't matter how many tabs there are in the rest of the line, they will never be acted on, with or without the 'g' modifier, since only one part of the string, the beginning, will ever match.
I hope this helps. As a first time poster, I'd appreciate any suggestions or feedback. But gently;-) please.
In reply to Re: Re: Re: whitespaces replace with tabs - help needed...
by ramjr
in thread whitespaces replace with tabs - help needed...
by star7
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |