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.
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.
$data =~ s/\s+/\t/g
You're welcome. (Specific question, high rate of accepting answers, what more could you want...)
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.
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