User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Help Perl Whitespaces

  #1  
Jun 7th, 2005
How does Perl count the number of whitespaces in a file?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Perl Whitespaces

  #2  
Jun 7th, 2005
I don't understand exactly what you mean. I can tell you that spaces, tabs, and newline characters qualify as white spaces..... if you would like sample code, that tells you how to count the number of white spaces.... then that's a different story.
Reply With Quote  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Perl Whitespaces

  #3  
Jun 8th, 2005
I am reading a file and trying to make a string based on the number of spaces that precedes the character that will go in the string. Does perl count each space as one character, or does it even count them at all?
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Perl Whitespaces

  #4  
Jun 8th, 2005
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....
$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.
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Perl Whitespaces

  #5  
Jun 8th, 2005
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....
Reply With Quote  
Join Date: Jun 2005
Posts: 17
Reputation: bigfoot_80906 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
bigfoot_80906 bigfoot_80906 is offline Offline
Newbie Poster

Re: Perl Whitespaces

  #6  
Jun 8th, 2005
Thanks a lot! That helps more than you can know!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Perl Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 10:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC