CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Urgent!!!!!!!!

 



yotam_k
Deleted

Mar 28, 2001, 11:12 AM

Post #1 of 8 (984 views)
Urgent!!!!!!!! Can't Post

could someone build for me a function that splits a $STRING and than count how much parts there is in $STRING? the "border" between two parts is " . "

ThanX!!!!!!!!!

Yotam.



Jean
User


Mar 28, 2001, 1:17 PM

Post #2 of 8 (980 views)
Re: Urgent!!!!!!!! [In reply to] Can't Post

In scalar context split() returns number of ocurrences,
e.g. $count = split(/\./, $string) will return number of tokens delimited by "." in $string.

Hope this helps.


Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


Jean
User


Mar 28, 2001, 1:23 PM

Post #3 of 8 (979 views)
Re: Urgent!!!!!!!! [In reply to] Can't Post

You can even use a structure like
$count = @tokens = split(/\./, $string);
in that case @tokens will contain the substrings delimited by "."
and $count will contain the number of resulting tokens.

That's surely is what you were looking for.
And all in one line - that's Perl.

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


Jasmine
Administrator

Mar 28, 2001, 2:44 PM

Post #4 of 8 (977 views)
Re: Urgent!!!!!!!! [In reply to] Can't Post

Holding true to TIMTOWDI (there's more than one way to do it), another way to count occurances of something in a string is by using tr///, which probably the most efficient way to go.

You can use any of the noted methods in a "normal" script, but if you're (let's say) counting the number of sentences in War and Peace, you'll probably want to use tr.

Whenever in doubt on what's the most efficient way to do something in Perl, use the Benchmark module:


Code
#!/usr/bin/perl 

use Benchmark;

$count = 500_000;

$string = 'Sentence 1. Sentence 2. Sentence 3.';

timethese($count, {
'split array' => sub {
$count = @tokens = split( /\./, $string );
},
'split into @_' => sub {
$count = split( /\./, $string );
},
'using tr' => sub {
$count = ( $string =~ tr/\.// );
},
}
);

Running this code outputs the following:


Code
#output 

Benchmark: timing 500000 iterations of split array, split into @_, using tr...
split array: 3 wallclock secs ( 2.03 usr + 0.00 sys = 2.03 CPU)
split into @_: 1 wallclock secs ( 1.82 usr + 0.00 sys = 1.82 CPU)
using tr: 0 wallclock secs ( 0.72 usr + 0.00 sys = 0.72 CPU)

(note that split into @_ will give a warning that this is deprecated).




Jean
User


Mar 28, 2001, 3:08 PM

Post #5 of 8 (974 views)
Re: Urgent!!!!!!!! [In reply to] Can't Post

Jasmine, I wouldn't like to sound overprotective of my suggestion, but what you're suggesting is not exactly what was asked in the original post - the idea is to count tokens and not the periods. Using tr/// will return wrong results in most cases, except when '.' is found in the beginning or the end of the line. (If not for these cases you could add 1 to the received result, but under circumstances it's not possible...)


Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


Jasmine
Administrator

Mar 28, 2001, 11:02 PM

Post #6 of 8 (969 views)
Re: Urgent!!!!!!!! [In reply to] Can't Post

Perhaps only yotam can answer this, but was it meant to count the tokens/periods or to count the number of elements that was separated by periods? If he were to use tr///, then it would count the number of tokens/periods, and if he were to use split, it would could the number of elements separated by those tokens/periods/borders.

Your reply was not "overprotective" at all Smile. Please, never worry to correct me when I may be misunderstanding a question. After all, the goal is to provide assistance :)

I hope our answers (singularly or collectively) help.

And I may not have mentioned anything before, Jean, but you are very helpful in these forums, and I thank you for your time Smile



yotam_k
Deleted

Mar 28, 2001, 11:57 PM

Post #7 of 8 (966 views)
Re: Urgent!!!!!!!! [In reply to] Can't Post

Thanx you all !
Jasmine: I'm afraid that what I wanted to count was the elements seperated by the " . " and not the number of seprators , altough I think I might use this in the futur...
So , thanx
and to Jean :
Thank you , you were very helpful !

Yotam.



Jean
User


Mar 29, 2001, 12:54 AM

Post #8 of 8 (965 views)
Re: Urgent!!!!!!!! [In reply to] Can't Post

Thanks and glad to "hear" that ;-)

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives