Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: how to get length of each line of file with spaces

by davido (Cardinal)
on May 17, 2019 at 14:23 UTC ( [id://11100176]=note: print w/replies, xml ) Need Help??


in reply to how to get length of each line of file with spaces

You could use something like my $l = length($string =~ s/\t/    /gr), though that's rather inefficient. Another solution:

my $tablength = 4; my $string = "\tSample\ttext."; my $expanded_length = render_length($string, $tablength); sub render_length { my ($string, $tabl) = @_; my $length = length($string); my $tabcount = $string =~ tr/\t//; $length += ($tabl - 1) * $tabcount; return $length; }

This isn't terribly elegant (wrote it on the train on the way to work), but it should work fine, except that the entire notion of tab length is a choice that you make, and convention is ambiguous.


Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11100176]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found