tags:

views:

110

answers:

3

Hi this is the sample text file with values , i need to remove the spaces and add the tabs could any one guide me the process?

Alpha : 12  13   14   15
Beta  : 14  45   56   67
Gama  : 89  98   00   00

Thanks in advance.

+6  A: 

$data =~ s/\s+/\t/g

You're welcome. (Specific question, high rate of accepting answers, what more could you want...)

jrockway
You don't want to turn newlines into tabs, though. :)
brian d foy
A: 

You need to "remove spaces and add tabs"?

This will do exactly what you specify:

my $foo = join "\t", grep $_ ne ' ', split '', $string;

It removes spaces and adds tabs. Did this work like you wanted? Why not?

You post a lot of very basic questions without any sample code or sign of effort on your part. So far, this has worked out well for you--you've gotten working code. But ask yourself this question: are you happy getting by on the charity of others?

This site is a wonderful resource for learning. You can abuse it and beg and cheat your way through your tasks, homework or other assignments. But will you progress in your abilities?

Spending some time reading Perl's copious docs won't hurt you. Neither would reading books like Learning Perl or The Perl Cookbook. There are resources like PLEAC that can help you with sample code for different tasks.

You could even try writing some code yourself. If it doesn't work, post it here and someone will help you fix it.

daotoad
Why are you doing so much work? Are you avoiding s/// or tr/// for some reason?
brian d foy
@brian, the bogus approach is part of the point. The code does "something mysterious" to remove spaces and adds tabs. The obvious answer is to use s///, as jrockway did. IMO, FRESHTER needs to write some code instead of asking others to do it for him. All I want to see is a sign of some attempt and figuring it out. He could have said "I think I should use the sub operator, but I am confused by TFM." or I tried this <broken code> and I get this error, what am I doing wrong? Sites like SO and Perlmonks should not be homework completion services.
daotoad
I agree that these sites should not be homework completion sites, but you're putting bad code out there because there's one person who's abusing it. Now everyone gets to see you're bad answer, and they don't know that it's bad. Your answer would be much better without the red herring. Remember, the biggest benefit of stackoverflow is people coming in from Google, so don't screw those people stuff that you know is the wrong answer.
brian d foy
@brian, I say "Did this work like you wanted? Why not?" I point out the fact that the code is probably not going to work as OP intends and ask him to consider why. The specification is met, the implied spec is not. This is a lesson of the importance of understanding your specification, and the code you run (especially when picked up from web fora). The combination of: harmless code (does not `rm -rf /` or any such nonsense), the fact that it meets spec, and that I lead the OP to the fact that the code may not do what he intends make this a cheap way to learn these lessons.
daotoad
+3  A: 

With Perl 5.10, you can use the new \h character class that matches only horizontal whitespace:

 use 5.010;
 while( <> ) {
     s/\h+/\t/; # turn all horizontal whitespace runs into tabs
     print;     # it's ready to print
     }

As a Perl one-liner, that's just:

 % perl -pe 's/\h+/\n/' file.txt

However, the transliteration operator is also very nice for this:

 while( <> ) {
     tr/ /\t/s;  # turn all spaces runs into tabs, squashing duplicates
     print;      # it's ready to print
     }

As a Perl one-liner, that's just almost just like sed:

 % perl -pe 'tr/ /\t/s' file.txt
brian d foy