Finding a string in an other string, then..

J

Joel

I have a text $alltext, and want to find a specific string
$stringtofind that is in the text.

Then, I want the script to read a number that is right after the
string.

What is the best way to get it ?

Thanks !
 
B

Beable van Polasm

Joel said:
I have a text $alltext, and want to find a specific string
$stringtofind that is in the text.

Then, I want the script to read a number that is right after the
string.

This sounds like a job for... Regular Expressions! Please read these
documents:

perldoc perlrequick
perldoc perlretut
perldoc perlre
What is the best way to get it ?

It depends upon your definition of "number", and so on. This
program might do something like what you want.

#!/usr/bin/perl

use strict;
use warnings;

my $alltext = "some string with some stuff in it 34454 and a number";
my $stringtofind = "stuff in it ";

if ($alltext =~ m/$stringtofind(\d+)/)
{
my $number = $1;
print(" found string, number is $number\n");
}
else
{
print("string not found\n");
}

__END__
 
J

Joel

Yes it worked already like that, but the problem is that my number tis
a DECIMAL one (xx.yyyy) and I get only the "xx" with this method !


Thanks,

Joel
 
T

Tad McClellan

[ Please do not post upside-down.
Text rearranged into actual chronological order.
Please do not quote an entire article.
]


^^^^^
^^^^^ "number" is specified here
Yes it worked already like that, but the problem is that my number tis
a DECIMAL one (xx.yyyy) and I get only the "xx" with this method !


Then change the method so as to accomodate your definition of "number".

Show us what you have tried so far, and we will help you fix it.

The Perl FAQ has regexes for various definitions of "number".

perldoc -q number

How do I determine whether a scalar is a number/whole/integer/float?
 
G

gnari

Joel said:
Yes it worked already like that, but the problem is that my number tis
a DECIMAL one (xx.yyyy) and I get only the "xx" with this method !

so what did you try to fix that, and how did it fail ?

[snipped WHOLE message quotes]

gnari
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top