Finding a value in a string 
Author Message
 Finding a value in a string


Quote:
>  I am reading a text file and I need to search for a string in the
>file and get the value after the string.  The string I am searching
>for is BegMessageXX where XX is a number.  I can search the string for
>BegMessage but how do I find out what the number after that string is
>?

>  I am already doing s/^BegMessage*/i  to find out if the line has
>that string on it, there is no space between the number and the text
>which why I am stuck or I would search for a space.

hmm...

   #store the matching number in a memory variable!
   $file =~ m/^BegMessage(\d+)/m;

   $number = $1;

good luck :)

--

CGI Meta FAQ <URL: http://www.*-*-*.com/ ;
Comprehensive Perl Archive Network (CPAN) <URL: http://www.*-*-*.com/ >
Perl Mongers Travel Deals! <URL: http://www.*-*-*.com/ ;



Fri, 22 Dec 2000 03:00:00 GMT  
 Finding a value in a string

  I am reading a text file and I need to search for a string in the
file and get the value after the string.  The string I am searching
for is BegMessageXX where XX is a number.  I can search the string for
BegMessage but how do I find out what the number after that string is
?

  I am already doing s/^BegMessage*/i  to find out if the line has
that string on it, there is no space between the number and the text
which why I am stuck or I would search for a space.

        Thanks for any help, sorry about the hotmail accout but this
way I can ignore the spammers.

        mike



Sat, 23 Dec 2000 03:00:00 GMT  
 Finding a value in a string

  Thanks to all that have replied to email and this group.  You all
guessed right that I was looking for numbers at the end of a string
and the m/^BegMessage(\d+)/ is what I needed, its now working.

 I feel stupid, I was stuck on getting a string when I really wanted a
number in a string.  But even so I am fairly new to perl and didnt
RTFM enough about m// to see that it would work.  Its the \d+ that did
the trick.

  Thanks...

        mike



Sat, 23 Dec 2000 03:00:00 GMT  
 Finding a value in a string

:    #store the matching number in a memory variable!
:    $file =~ m/^BegMessage(\d+)/m;
:
:    $number = $1;

Or save yourself a line and unneeded $1 reference:

  ($number) = $file =~ m/^BegMessage(\d+)/m;

---------------------------------------------------------------------

 --*--    Home Page: http://www.cinenet.net/users/cberry/home.html
   |      Member of The HTML Writers Guild: http://www.hwg.org/  
       "Every man and every woman is a star."



Sat, 23 Dec 2000 03:00:00 GMT  
 Finding a value in a string
Quote:

>   I am reading a text file and I need to search for a string in the
> file and get the value after the string.  The string I am searching
> for is BegMessageXX where XX is a number.  I can search the string for
> BegMessage but how do I find out what the number after that string is
> ?

>   I am already doing s/^BegMessage*/i  to find out if the line has

I don't think that means what you think it means.

You've confused perl, and me.  That would be an unterminated search
pattern.  Perhaps you meant m//i?  You'd still be left with the problem
of extracting the number.

Quote:
> that string on it, there is no space between the number and the text
> which why I am stuck or I would search for a space.

if ($string =~ /^BegMessage(\d{2})/i) {
        print "Number is $1\n";

Quote:
}

Lets look at this for a second:
/               Start regex
^               Anchor at beginning of line
BegMessage      search for the characters BegMessage
(               First matching pattern to remember - stored in $1 by perl
\d              Any digit 0-9
{2}             Exactly 2 of them
)               End first matching pattern
/i              End regex, ignore case

When I set $string = 'BegMessage84', I get:
Number is 84
as output.

Is that what you were after?

I would suggest a read through perldoc perlre for more insight, and help
on the above.

Quote:

>         Thanks for any help, sorry about the hotmail accout but this
> way I can ignore the spammers.

>         mike

HTH.

Dave

--
"Security through obscurity is no security at all."
                -comp.lang.perl.misc newsgroup posting

----------------------------------------------------------------------


----------------------------------------------------------------------



Sat, 23 Dec 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. finding files containing string x but not string y

2. PROPOSAL/ANNOUNCE - String::Trigram (Finding similar strings)

3. Finding a string within a string

4. finding the last occurrence of a given value

5. How to find existence of a hash value.

6. finding largest repeat values in array

7. finding largest repreat value in an array

8. finding largest repeat value in array

9. finding max and min value

10. Finding Arc-cos -or- returning value from system call

11. ARRAYS: find common values

12. Question: optimized method for finding the maximum value in an array

 

 
Powered by phpBB® Forum Software