Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate variable with piece of string? 1

Status
Not open for further replies.

mstelmac

Technical User
Oct 22, 2003
53
0
0
US
Hello everyone. Can anyone tell me the best way to populate a variable with a piece of a string. I need to capture all of the text after the last under score.

my $string = "3.0.0_SWNAME_48"

I'm really just trying to populate a variable with the number 48. Problem is 48 could really be 480, or 481, or any number. So I really just need to capture everything after the last underscore because it could be any number greater than 1.

Thanks in advance for any suggestions.
 
my $var = "3.0.0_SWNAME_48";
my @tmp = split(/\_/, $var);
my $string = $tmp[-1];




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thank you both very much. I was going down the wrong path and using a substr command. This is very useful.

thanks again.
-Matt
 
to avoid false matches:

Code:
my $var = "3.0.0_SWNAME_48";
my $string = '';
if ($var =~ /_(\d+)$/) {
   $string = $1;
}

if that is not a worry you can use any of the suggestions.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
And in addition to using the regex, throw in your error checking call:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$var[/blue] = [red]"[/red][purple]3.0.0_SWNAME_48[/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$string[/blue] = [red]([/red][blue]$var[/blue] =~ [red]/[/red][purple]_([purple][b]\d[/b][/purple]+)$[/purple][red]/[/red][red])[/red]
	? [blue]$1[/blue]
	: [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Pattern not found in string: '[blue]$var[/blue]'[/purple][red]"[/red][red];[/red]
- Miller
 
No need for a temporary array simply:
Code:
my $var = "3.0.0_SWNAME_48";
my $string = (split(/\_/, $var) )[-1];

Or alternatively:
Code:
$string = substr( $var, rindex( $var, '_' ) + 1 );
print "$string\n";

Or...
Code:
( $string ) = $var =~ /_(\d+)$/;
print "$string\n";
 
I have a feeling this is a slow alternative:

Code:
$string = substr( $var, rindex( $var, '_' ) + 1 );
print "$string\n";

two functions are needed to get the data.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
On the contrary, it's quite a bit faster, as it doesn't need the regexp engine:
Code:
#!/usr/bin/perl -w
use strict;
use Benchmark qw(:all);

my $var = "3.0.0_SWNAME_48";

cmpthese ( 1000000, {
   split => sub { my $string = (split(/\_/, $var) )[-1] },
   substr => sub { my $string = substr( $var, rindex( $var, '_' ) + 1 ) },
   regexp => sub { my ( $string ) = $var =~ /_(\d+)$/ }
});

Output:
Code:
            Rate regexp  split substr
regexp  625000/s     --   -13%   -71%
split   719424/s    15%     --   -67%
substr 2173913/s   248%   202%     --
 
I stand corrected [purpleface] [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You guys are scary sometimes you know that!! :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top