•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 392,021 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,267 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Views: 5226 | Replies: 5
![]() |
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 107
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 107
Here is some code for you to count the number of "white spaces" in a file at the end of the string... the sub (function) classifies white spaces as space, tab, and newline.... you can modify the source accordingly, should it only be spaces, or only be tabs, or both... or any combo....
Hope This Helps.
$file = $ARGV[0];
if ($file eq "") {
exit;
}
$wspace = 0;
open (FH, "$file");
while (<FH>) {
$nums = &count_trailing_white_spaces($_);
$wspace += $nums;
}
close(FH);
print "There Are: $wspace White Spaces\n";
exit;
sub count_trailing_white_spaces
{
$thestring = shift;
my $wspacecnt = 0;
while (substr($thestring, length($thestring) -1, 1) eq " " || substr($thestring, length($thestring) -1, 1) eq "\t" || substr($thestring, length($thestring) -1, 1) eq "\n") {
$wspacecnt++;
chop($thestring);
}
return $wspacecnt;
}Hope This Helps.
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 107
Yes, Perl Counts Spaces as 1 character...and you can modify the above sub to count from the begining of the string, and loop until it finds a normal character. The one above finds trailing white spaces.... so, it starts from the very right of the string, and loops until there are no more tabs, spaces, or newline characters trailing "normal characters". As soon as the loop reaches something that is NOT a space or a tab or a newline character, it stops the loop. To have the loop start from the begining, then you would have to modify the loop quite a bit... because I use chop to shorten the string after the white space is counted... you would have to make your own function to chop the string at the begining and work that way....
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
Similar Threads
- Formatting in perl (Perl)
- OptiPerl - Perl IDE (Perl)
Other Threads in the Perl Forum
- Previous Thread: Please help to modify this perl script. I need to add more urls
- Next Thread: Log file rotation script



Linear Mode