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: Regular Expressions:
Find string of single digits in string of numbers

 



gobbluth529
New User

Jan 15, 2010, 10:07 PM

Post #1 of 3 (2960 views)
Find string of single digits in string of numbers Can't Post

I have several line of numbers for which I need to extract the single digits. All numbers are separated by spaces. Example:
28 5 29 29 5 5 27 30 5 29 34 34 34 33 29 5 30 34 34 34 33 34 34 34 34 33 34 33 34 34 34 30 26 33 34 33 26 32 33 33 33 33 34 33 33 34 33 26 33 33 33 33 32 31 34 32 33 32 5 4 7 8 3 6 8 4 33 31 33 33 27 33 30 5 30 32 26 4
As you can see there is a set of single digits nested within the string 5 4 7 8 3 6 8 4. these numbers will change and it does not matter what the numbers are as long as they are single digits. I can match that by doing
m/\s\d\s\d\s\d\s\d\s\d\s\d\s\d\s\d/
But the number of single digits that are strung together will also change, and I would like to specify that as a variable.
I want to do something like this:
m/<\s\d\s>{$t,}/
but that of course won't work. I want to be able to change $t to various numbers without changing the script. Any help with this would be greatly appreciated. I do not think using the word boundaries will work because they are digits and

Thanks.


FishMonger
Veteran / Moderator

Jan 16, 2010, 6:00 AM

Post #2 of 3 (2952 views)
Re: [gobbluth529] Find string of single digits in string of numbers [In reply to] Can't Post


Code
use Data::Dumper; 

my $str = '28 5 29 29 5 5 27 30 5 29 34 34 34 33 29 5 30 34 34 34 33 34 34 34 34 33 34 33 34 34 34 30 26 33 34 33 26 32 33 33 33 33 34 33 33 34 33 26 33 33 33 33 32 31 34 32 33 32 5 4 7 8 3 6 8 4 33 31 33 33 27 33 30 5 30 32 26 4';
my @num = $str =~ /\b(\d)\b/g;
print Dumper \@num;


Outputs:

Code
$VAR1 = [ 
'5',
'5',
'5',
'5',
'5',
'5',
'4',
'7',
'8',
'3',
'6',
'8',
'4',
'5',
'4'
];



shawnhcorey
Enthusiast


Jan 16, 2010, 6:03 AM

Post #3 of 3 (2951 views)
Re: [gobbluth529] Find string of single digits in string of numbers [In reply to] Can't Post


Code
m{ \b \d \s* $t \b }msx;


See `perldoc perlre`

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


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

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