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

how to get length of each line of file with spaces

by phoenix007 (Sexton)
on May 17, 2019 at 12:43 UTC ( [id://11100168]=perlquestion: print w/replies, xml ) Need Help??

phoenix007 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I am trying to find out length of each line of file. I am using length function. Problem is length function is counting tab as a single char and not as 4 chars

input file :

This line has no tab. This line has tab at the beginning.

Script I am using :

open(my $fh, '<', $inputfile); my %line_chars; my $line_number = 1; while (my $row = <$fh>) { if ($line_number > 1) { $line_chars{$line_number} = $line_chars{$line_number - +1} + length($row); } else { $line_chars{$line_number} = length($row); } $line_number++; }

Here I need output with all spaces before and after line. specially when line starts with tab

Replies are listed 'Best First'.
Re: how to get length of each line of file with spaces
by Fletch (Bishop) on May 17, 2019 at 12:47 UTC

    Because a TAB is a single ASCII character represented by one octet / byte (pay no attention to the Unicode behind the curtain . . .); it's only by convention that it's displayed as four (or two, or eight, or . . .). You'd need to do something to expand it to spaces (e.g. s{\t}{    }gx) before you count if you're going to use length.

    Edit: Also, ALWAYS CHECK THE RETURN VALUE FROM open.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Except when using autodie (in which case open doesn't return anything on failure I suppose?)

        Except when using autodie (in which case open doesn't return anything on failure I suppose?)

        Right. The open wrapper created by autodie dies instead of returning. (This happens somewhere in Fatal, from which autodie inherits almost everything.)

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: how to get length of each line of file with spaces
by AnomalousMonk (Archbishop) on May 17, 2019 at 15:35 UTC

    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:  <%-{-{-{-<

Re: how to get length of each line of file with spaces
by davido (Cardinal) on May 17, 2019 at 14:23 UTC

    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

Re: how to get length of each line of file with spaces
by Laurent_R (Canon) on May 17, 2019 at 17:59 UTC
    You've been given some good solutions already, and my guess is that if you use a regex substitution (s/\t/    /g) or the tr//: transliteration operator, you probably no longer need to keep track of the line number. But if you need to track the line number for some other reason, please be advised that the $. special variable does that automatically for you (it reports the current input line number of the last file handle that was read).
Re: how to get length of each line of file with spaces
by Your Mother (Archbishop) on May 17, 2019 at 17:54 UTC

    You already got good functional answers so grumpy typist/typesetter time. This concept of tabs is infuriating. A tab is a single marker of *someplace* on the page, not necessarily four characters at all. Now, get off my lawn!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11100168]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found