That only matches a space. The term 'whitespace' is much broader than a space--it includes things like newlines, tabs, etc. You want to match using \s, which matches any whitespace character.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] [d/l] |
| [reply] [d/l] |
Thanks. I realized that right after I clicked the submit button. Then I noticed someone else posted the s/\s/\t/g so I didn't bother updating my node. I'm new here, what is the policy? Should I fix my nodes even when someone beats me to the punch?
| [reply] |
Typically, a change to your node is done by putting a paragraph starting with a bold Update, followed by a note of the changes made.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] |
That*s the way i like it :)
i wrote following code:
LINE: while(<FILE>) {
chomp($_);
$_=~ s/ +/\t/s;
print OUTPUT "$_\n";
# $_ =~ s/^(d{6})\t(d{2}):(d{2})/$1 $2:$3/g;
};
close(FILE);
close(OUTPUT);
(maybe i have to use the option e)
What i want:
1. replace one or more spaces with a tab
2. see #comment: in the line (-> $_ ) replace first tab with a space
Maybe you are more sage as i.
can you help me to adjust this code.
Thanks.
star7
| [reply] [d/l] |
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 tab
2. see #comment: in the line (-> $_ ) replace first tab with a space
But 1 says the reverse of what 2 says, if I'm reading it correctly.
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.
| [reply] [d/l] [select] |
Hi,
you may want to take a look at tr// (`translate'), especially the `s' modifier. This will replace multiple duplicated characters with one instance of the required character.
For example, in your line $_=~s/ +/\t/s;,
(the $_ isn't really necessary here) you could have tr/ /\t/s;. I understand it's quite a bit more efficient.
| [reply] [d/l] [select] |