Re: How do I replace \t and regarding to the alignment requirement ?
by BrowserUk (Patriarch) on Apr 19, 2012 at 14:06 UTC
|
$s = "The\tquick\tbrown\tfox\tjumps\tover\tthe\tlazy\tdog";;
print $s;;
The quick brown fox jumps over the lazy dog
($t=$s);
1 while $t =~ s[(\t)]{ '.' x ( 8 - ( $-[0] % 8 ) ) }e;
print $t;;
The.....quick...brown...fox.....jumps...over....the.....lazy....dog
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [d/l] |
Re: How do I replace \t and regarding to the alignment requirement ?
by Corion (Patriarch) on Apr 19, 2012 at 13:34 UTC
|
Searching for tab expand and tabs only yields the expand and unexpand programs in ppt. They even do seem to support arbitrary tab stops. I would look at ripping the code out of one of these, as there doesn't seem to be a Text:: module that provides that functionality.
Aaahah. Searching for tabstop finds Text::Format, which claims to have ->expand as a subroutine.
| [reply] [d/l] [select] |
|
expand blindly replaces 1 tab with 8 spaces,so it's not fit for this task...
| [reply] [d/l] |
|
Q:\>copy con test.txt
blah a blah
^Z
1 file(s) copied.
Q:\>perl -w expand.pl test.txt
blah a blah
Q:\>perl -w expand.pl test.txt|perl -ple "s!\t!x!g"
blah a blah
Q:\>perl -w expand.pl test.txt|perl -ple "s! !_!g"
blah____a_______blah
Q:\>type test.txt|perl -ple "s! !_!g"
blah a blah
If expand is not working for you, maybe the code I linked to does work for you and you can reuse that? | [reply] [d/l] [select] |
Re: How do I replace \t and regarding to the alignment requirement ?
by JavaFan (Canon) on Apr 19, 2012 at 13:47 UTC
|
my @l = (0, 7, 6, 5, 4, 3, 2, 1);
$str =~ s/\G([^\t]*)\t/$1 . (' ' x $l[length($1) % 8])/eg;
I'm sure it can be done with a closed formula instead of a lookup table, but this was faster coding. | [reply] [d/l] |
|
This doesn't work for multiple lines.
| [reply] |
|
| [reply] |
|
|
|
|
| [reply] |
Re: How do I replace \t and regarding to the alignment requirement ?
by Utilitarian (Vicar) on Apr 19, 2012 at 14:27 UTC
|
And without using the @LAST_MATCH_START variable, because I thought it was an evil which tainted any other regex used in the program (though I may be completely mistaken in this)
perl -Mstrict -we 'my $string="random\ttab\tseparated\twords";
sub tab2space{my ($string,$spacing)=@_;
while($string=~/\\t/){
print $string,"\n";
my $index=index($string,"\\t");
$string=~s/\\t/" " x ($index %8)/e;
}
return $string}
for (0..80){print $_ % 10;}
print "\n", tab2space($string) , "\n";'
0123456789012345678901234567890123456789012345678901234567890123456789
+01234567890
random tab separated words
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
| [reply] [d/l] [select] |
|
... @LAST_MATCH_START variable ... an evil which tainted any other regex ...
You may be thinking of $& ($MATCH) and friends ($` and $'). While @LAST_MATCH_START (@-) may share in the general overhead associated with capturing, I am not aware that it is touched by the evil of $& and its ilk. And with 5.10+, the incantation of the /p regex modifier can raise even $` $& $' to purity as the ${^PREMATCH} ${^MATCH} ${^POSTMATCH} special variables. Oh, happy day!
| [reply] [d/l] [select] |
Re: How do I replace \t and regarding to the alignment requirement ?
by Anonymous Monk on Apr 19, 2012 at 13:33 UTC
|
my $input = "blah\tblah\tblah";
my $wanted = ...;
FWIW, a regex can't do nothing, substitution operator does stuff
| [reply] [d/l] |
|
$ perl -MPerl6::Form -le " print form q{{:[{8}]:}} x 3, qw/ blah a bla
+h / "
blah a blah
12345678
12345678
12345678
| [reply] [d/l] [select] |