At the beginning I simply used the following to count the length of each line:
while(<FH>){
chomp;
$length=length($_);
}
but when I compared the result I got with the one produced by linux command WC, I found a problem:
all tab characters in my file are treated as of 1 character
length in perl, whereas it is 8
for wc
, so I did the following modification:
while(<FH>){
chomp;
my $length=length($_);
my $tabCount= tr/\t/\t/;
my $lineLength=$wc-$tabCount+($tabCount*8);
}
for the above code it works for all most all the cases now, except for one, in wc
not all tabs are counted, but only the one that has not be taken with some characters, for example, if at the start of a line, I type in1234
and then press a tab, in wc
it is not counted as a tab, but the above code counted that, are there any ways I could adopt to solve this issue? Thanks
Solved it, used tab expansion, here is the code:
1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
$length=length($string);
if anyone could give it an explanation, that would be awesome, I tested it to be working, but don't quite understand it. Anyways, thanks for all the help
Solved it, used tab expansion, here is the code:
1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
$length=length($string);
if anyone could give it an explanation, that would be awesome, I tested it to be working, but don't quite understand it. Anyways, thanks for all the help