sufi has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Replace only tab space with commas
by davido (Cardinal) on Mar 31, 2011 at 06:03 UTC

    The following documents should take you two or three hours to read through. After that, it will not be necessary for you to ask us every time you need a simple regexp solution to parsing a simple text file.

    If after that you still need additional info you can look at the following:

    Ok, that's going to be another four or five hours of reading. But you want to learn this stuff right? Eight hours of free documentation to read and you'll avoid having to ask when your specification changes to want to replace a space followed by a newline with a comma and an asterisk. My point is that each of the questions you've asked may indeed be different questions. But it's like asking, "Now what if I want the elevator to go to the third floor, what do I do then?" And then a day later, "But what if I want it to go to the fourth floor? How would I do that?" And then... "Ok, here's a good one for you. What if I want the elevator to go to the fifth floor?" You can do better than that, I'm confident.


    Dave

Re: Replace only tab space with commas
by cdarke (Prior) on Mar 31, 2011 at 07:15 UTC
    The length of a tab is not defined across applications, so it is not necessarily the same size as 8 spaces. A tab is a single character, usually expressed in most programming languages as "\t".

    There are several characters which could come under the heading "white-space", that includes tabs and spaces, but also new-lines "\n", carriage-returns "\r", form-feeds "\f" and sometimes more. If you wish to include all white-space in you regular expression then use the \s short-cut as described by Nikhil Jain, otherwise just use s/\t/,/g. I'm guessing that
    I don't want to replace a single character character or any non tab space with comma means you need \t, not \s.
Re: Replace only tab space with commas
by Anonymous Monk on Mar 31, 2011 at 04:10 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Replace only tab space with commas
by dasgar (Priest) on Mar 31, 2011 at 05:51 UTC

    Perhaps in my sleep deprived state I'm overlooking something, but I believe that following work:

    use strict; use warnings; my $text = "I love\tmy nation."; $text =~ s/\t/,/g; print "$text";

    Output: I love,my nation.

    However, if those are actually 8 spaces instead of an actual tab, the regex in my code won't work. In that case, you'd probably want to do something along the lines of what Nikhil Jain posted.

Re: Replace only tab space with commas
by Nikhil Jain (Monk) on Mar 31, 2011 at 04:31 UTC
    use strict; use warnings; my $string = "I love my nation"; #leaving one space and substitute 2 or more spaces with comma globally $string =~ s/\s{2,}/,/g; print "$string\n";

    Output:

    I love,my nation