Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by AnomalousMonk (Archbishop)
on May 17, 2019 at 15:35 UTC ( [id://11100182]=note: print w/replies, xml ) Need Help??


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

Using  tr/// as davido suggests can very efficiently count characters and also exclude characters from a count with the  /c modifier (see  tr/// in the Quote-Like Operators section of perlop):

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qq(pp); ;; my $entab = 4; ;; my @lines = ( qq{This line has no tab.\n}, qq{\tThis line has tab at the beginning.\n} ); ;; for my $line (@lines) { my $rend = ($line =~ tr/\t//c) + ($entab * $line =~ tr/\t//); printf qq{%s renders as %d chars long \n}, pp($line), $rend; } " "This line has no tab.\n" renders as 22 chars long "\tThis line has tab at the beginning.\n" renders as 40 chars long


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found