Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by monktim (Friar)
on Aug 14, 2003 at 13:38 UTC ( [id://283868]=note: print w/replies, xml ) Need Help??


in reply to whitespaces replace with tabs - help needed...

$str =~ s/ /\t/g;

Replies are listed 'Best First'.
Re: Re: whitespaces replace with tabs - help needed...
by hardburn (Abbot) on Aug 14, 2003 at 13:47 UTC

    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

      hardburn is right about what constitutes a whitespace, but I suspect that star7 may not be using the term correctly.
      I'm also confused, star7, when you say tabs are the problem. You seem to be putting tabs into your document. If they are the problem, wouldn't you be trying to remove them?

      $str =~ s/\t/ /g;

      Replacing each tab with a space. (not whitespace)

      -ted-

      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?

        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

Re: Re: whitespaces replace with tabs - help needed...
by star7 (Novice) on Aug 14, 2003 at 15:00 UTC
    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

      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.

      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://283868]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found