Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I replace \t and regarding to the alignment requirement ?

by PerlOnTheWay (Scribe)
on Apr 19, 2012 at 13:29 UTC ( #965952=perlquestion: print w/ replies, xml ) Need Help??
PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

I want to replace \t so that the next character starts at the next offset that equals 1 mod 8.

Seems not possible by a single regex, anyone has good ideas?

input :blah\ta\tblah

output:blah(4spaces)a(7spaces)blar

Comment on How do I replace \t and regarding to the alignment requirement ?
Select or Download Code
Re: How do I replace \t and regarding to the alignment requirement ?
by Anonymous Monk on Apr 19, 2012 at 13:33 UTC

    It is unclear what you're trying to accomplish

    my $input = "blah\tblah\tblah"; my $wanted = ...;

    FWIW, a regex can't do nothing, substitution operator does stuff

      input :blah\ta\tblah

      output:blah(4spaces)a(7spaces)blar

      :) That is not $input and $output :)

      $ perl -MPerl6::Form -le " print form q{{:[{8}]:}} x 3, qw/ blah a bla +h / " blah a blah
      12345678 12345678 12345678
Re: How do I replace \t and regarding to the alignment requirement ?
by Corion (Pope) 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.

      expand blindly replaces 1 tab with 8 spaces,so it's not fit for this task...

        Weird - at least this version of expand works for me, and the source code suggests that it should work just as it does:

        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?

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.
      This doesn't work for multiple lines.

        And what should it do for multiple lines?

        Sure it does.

        Unless you mean that you want to restart counting offsets on each newline. But you didn't specify that.

Re: How do I replace \t and regarding to the alignment requirement ?
by BrowserUk (Pope) on Apr 19, 2012 at 14:06 UTC

    Like this?

    $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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

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."
      ... @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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965952]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-09-20 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls