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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.