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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone figure out this warning!! 1

Status
Not open for further replies.

ZsN1Gman

Programmer
Aug 22, 2002
17
US
Here's a simple one!! But I don't get it?

I'm using the loop below to pull info from url's
Anyhow, this works fine. But with warnings on I'm getting about 75 copies of the warning: &quot;Use of uninitialized value in pattern match (m//) at URL_Finder.pl line 18, <FILE> line 315.&quot;

Line 18 is: @contents = (@contents, $2) if ($2 !~ /^$/);
Specifically perl seems to have a problem with the use of $2.

Line 315 is the last line in the <FILE>.

Can anyone fill me in?
Code:
my @fileContents;
for (<FILE>)
{
 chomp;
 if ($_ !~ /^$/)
 {
   $_ =~ m!(//)[w]*\.?([^/]+)!i;
   @fileContents = (@fileContents, $2)  if ($2 !~ /^$/);
 }
}
 
This means that the value $_ is not predeclared.

$_=undef; or try $_='';
my @fileContents;
for (<FILE>)
{
chomp;
if ($_ !~ /^$/)
{
$_ =~ m!(//)[w]*\.?([^/]+)!i;
@fileContents = (@fileContents, $2) if ($2 !~ /^$/);
}
}
This should stop the warning.

This line is attempting to match a regular expression if it failes PERL will
 
I appreciate the attempt, but that isn't doin it. I can't say that I've ever pre-declared a built-in before!

I find it interesting that the warning displays 75 times, yet around 300 iterations occur resulting in 200+ matches. And the reference to <FILE> line 315? 315 is the last line in the file and is empty.
 
a couple of thoughts

1 - Could it be that line 315 contains spaces? If you changed your &quot;is-it-a-blank-line&quot; check to /^\s*$/ that may well help.

2 - $2 isn't set because the match is failing. You can get rid of the error messages with this code. You'll notice that I'm using 'push' to add values to the array as well.

if( $_ =~ m!(//)[w]*\.?([^/]+)!i ){
push @fileContents, $2;
} else {
print &quot;ERROR: Line not matched: $_&quot;;
} Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
You were right about the warning occuring because of a failed match, and $2 not being set. That makes perfect sense.

And your solution works great.

I appreciate the help, Thanks!

Jim



 
:) Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
G - $2 is a built-in variable.

It, and $1, get's set after a regular expression match which includes to sets of parenthese (brackets &quot;()&quot; ), $2 is set to the matched text in the second set of brackets. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
I don't think you can match against the result of a regular expression without first assigning it to another variable, as it tries to set itself during the match. I think it would work if you did:
Code:
my $string = $2;
@fileContents = (@fileContents, $string) if ($string !~ /^$/);
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top