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:
Why am I having trouble using a variable in my regex

 



aueddonline
Novice

Oct 29, 2011, 10:12 AM

Post #1 of 8 (14226 views)
Why am I having trouble using a variable in my regex Can't Post

I'm searching a html document contained in $content.


Code
if ($content =~ m/($ticker)/) 
{
print "Found one";
}

When I run my code is doesn't 'find one'. if I change the variable to a text string it does 'find one' i.e.


Code
if ($content =~ m/(barc)/) 
{
print "Found one";
}


i I've tried changing the case of the string, this doesn't make a difference it still finds stuff.

if i print the variable it holds the string I expect

Confused!


FishMonger
Veteran / Moderator

Oct 29, 2011, 11:30 AM

Post #2 of 8 (14219 views)
Re: [aueddonline] Why am I having trouble using a variable in my regex [In reply to] Can't Post

Are you sure the vars hold what you think?

Use the Data::Dumper module to output the vars when comparing the values.

http://search.cpan.org/~smueller/Data-Dumper-2.131/Dumper.pm


aueddonline
Novice

Oct 30, 2011, 6:04 AM

Post #3 of 8 (14103 views)
Re: [FishMonger] Why am I having trouble using a variable in my regex [In reply to] Can't Post

no so sure now :)

What i've done is printed $myarray[10], which gives the value BTT.I on the screen. but then I've printed length($myarray[10]) and got the number 7, so there is some whitespace maybe?

but then i've tried '$anothervar = chomp($myarray[10])' then printing $anothervar, but I get no output from this.

Weird no? the text file i'm using has on value per line which are being pushed into the array


aueddonline
Novice

Oct 30, 2011, 6:31 AM

Post #4 of 8 (14100 views)
Re: [aueddonline] Why am I having trouble using a variable in my regex [In reply to] Can't Post

I found this sub which has fixed the problem, think there my have been whitespace at the start of the string

http://www.somacon.com/p114.php

In Reply To


FishMonger
Veteran / Moderator

Oct 30, 2011, 8:36 AM

Post #5 of 8 (14079 views)
Re: [aueddonline] Why am I having trouble using a variable in my regex [In reply to] Can't Post

If you had used Data::Dumper to inspect your vars as I suggested, you would have immediately discovered the problem.

The person that wrote that article is not very good at Perl. Using 3 separate subs with prototype definitions just to strip whitespace is nuts.


aueddonline
Novice

Oct 30, 2011, 10:39 AM

Post #6 of 8 (14060 views)
Re: [FishMonger] Why am I having trouble using a variable in my regex [In reply to] Can't Post

Thanks for suggesting Data Dumper. I have used it now and see that it would have shown me the problem quicker, in addition to that it's a useful tool for the future.

Something inside me says, geez, do I have to use another module, surely this is simple enough not to complicate, but that's my brain not quite working correctly.

Anyway

I just used one of the subs, trim(). Is there a method for perl that does this? I couldn't see one. or perhaps a better way?

looking at the dumper output there was some whitespace at the end plus a /n.


FishMonger
Veteran / Moderator

Oct 30, 2011, 1:11 PM

Post #7 of 8 (14037 views)
Re: [aueddonline] Why am I having trouble using a variable in my regex [In reply to] Can't Post

You need to show more of your code to provide the proper context, but I'll assume for now that you're reading in a file line-by-line into the $content var. If that's the case, then you'd do something like this:


Code
open my $fh, '<', 'somefile.txt' or die "can't open 'somefile.txt $!"; 

while ( my $content = <$fh> ) {

# strip off the \n line ending
chomp $content;

# strip leading and trailing spaces
$content =~ s/^\s+|\s+$//g;

# additional processing
}



aueddonline
Novice

Oct 31, 2011, 7:29 AM

Post #8 of 8 (13934 views)
Re: [FishMonger] Why am I having trouble using a variable in my regex [In reply to] Can't Post

You assume correct

Many thanks for you help

 
 


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

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