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: Beginner:
extraction of a substring out of a string

 



chomdang
Deleted

Aug 8, 2000, 1:26 PM

Post #1 of 4 (492 views)
extraction of a substring out of a string Can't Post

Hi every one...,
I have an other question....
I want to extract a substring out of a string.
Example: $string = '1001 boat';
I want to extract 1001. I tried this and it works : $test = substr($string, 0, 4);
But I want a better way, Could someone tell me how I could find the position between 1001 and boat and then, only retreive 1001 (the left part of the string). The reason is that I won't know the length of the string I wanna to extract but I know that there will be a space between the two substrings.
Thanks in advance
Dang


Jasmine
Administrator / Moderator

Aug 8, 2000, 1:50 PM

Post #2 of 4 (492 views)
Re: extraction of a substring out of a string [In reply to] Can't Post

If you know for certain that the delimiter will always be a space, and you know for certain that you'll always want the first part, you can use:

my $first = (split(/ /,$string))[0];

But why you'd want use anything but substr questionable -- substr is comparatively faster

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


Benchmark: timing 1000000 iterations of split, substr...
split: 5 wallclock secs ( 6.04 usr + 0.00 sys = 6.04 CPU)
substr: 0 wallclock secs ( 1.25 usr + 0.00 sys = 1.25 CPU)
</pre><HR></BLOCKQUOTE>

If anyone else has any ideas, please post'em.


Cure
User

Aug 8, 2000, 6:53 PM

Post #3 of 4 (492 views)
Re: extraction of a substring out of a string [In reply to] Can't Post

#!perl
use Benchmark;
$_='1000 perl';
timethese (500000,{
REGEX_UNTIL_THE_FIRST_NONDIGIT=>q!
$garbage= /(\d+)/;
!,REGEX_UNTIL_THE_FIRST_SPACE=>q!
$garbage= /(\S+)/;
!,SPLIT_BY_NONDIGIT=>q!
$garbage= +(split /\D/)[0];
!,SPLIT_BY_SPACE=>q!
$garbage= +(split /\s/)[0];
!,SUBSTR_N_INDEX=>q!
$garbage= substr $_,0,index $_,' ';
!,SUBSTR_WHEN_LENGTH_IS_KNOWN=>q!
$garbage= substr $_,0,4;
!});
##########################################

Benchmark: timing 500000 iterations of REGEX_UNTIL_THE_FIRST_NONDIGIT, REGEX_UNTIL_THE_FIRST_SPACE, SPLIT_BY_NONDIGIT, SPLIT_BY_SPACE, SUBSTR_N_INDEX, SUBSTR_WHEN_LENGTH_IS_KNOWN...

REGEX_UNTIL_THE_FIRST_NONDIGIT: 3 wallclock secs ( 3.60 usr + 0.00 sys = 3.60 CPU)

REGEX_UNTIL_THE_FIRST_SPACE: 3 wallclock secs ( 3.84 usr + 0.00 sys = 3.84 CPU)

SPLIT_BY_NONDIGIT: 5 wallclock secs ( 4.92 usr + 0.01 sys = 4.93 CPU)

SPLIT_BY_SPACE: 5 wallclock secs ( 5.14 usr + 0.00 sys = 5.14 CPU)

SUBSTR_N_INDEX: 7 wallclock secs ( 7.23 usr + 0.00 sys = 7.23 CPU)

SUBSTR_WHEN_LENGTH_IS_KNOWN: 7 wallclock secs ( 6.19 usr + 0.00 sys = 6.19 CPU)

Cure




chomdang
Deleted

Aug 9, 2000, 4:22 AM

Post #4 of 4 (492 views)
Re: extraction of a substring out of a string [In reply to] Can't Post

Thanks a lot for your help (both of you). This is the first day I'm using this forum and it is very good...
Thanks again
Dang

 
 


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

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