in reply to how to find the length of any line of file
Hello lakshmikant,
One possible way to get the length of a string is using the length function. I would also recommend to use warnings and strict as the fellow Monks already proposed. Also when opening and closing files use die or warn, read the links on why and how to use them.
Also when you open a file use the MODE for reading e.g. '<'.
Sample of solution to your problem including all the minor recommendations:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $file = 'myfilename.txt'; open my $fb, "<", $file or die "Can not open '$file': $!"; while (my $line=<$fb>) { chomp $line; say 'Line: ' . $line . ' Length: ' . length $line; } close $fb or warn "Can not close '$file': $!"; __END__ $ perl test.pl Line: heaven Length: 6 Line: heavenly Length: 8 Line: heavenns Length: 8 Line: abc Length: 3 Line: heavenns Length: 8 Line: heavennly Length: 9
Update: You should read about the perlvar/SPECIAL-VARIABLES regarding the $. variable that you are using on your script. Maybe you do not understand yet exactly how it works or what is it for.
Hope this helps, BR.
|
---|