Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: whitespaces replace with tabs - help needed...

by star7 (Novice)
on Aug 14, 2003 at 15:00 UTC ( [id://283900]=note: print w/replies, xml ) Need Help??


in reply to Re: whitespaces replace with tabs - help needed...
in thread whitespaces replace with tabs - help needed...

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

Replies are listed 'Best First'.
Re: Re: Re: whitespaces replace with tabs - help needed...
by ramjr (Initiate) on Aug 14, 2003 at 22:56 UTC

    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.

Re: Re: Re: whitespaces replace with tabs - help needed...
by hagen (Friar) on Aug 15, 2003 at 02:51 UTC

    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found