Solved

Perl IF statement

Posted on 1998-07-23
22
205 Views
Last Modified: 2010-03-05
I have written an if statement to go and search a file using the information contained in a variable.
The If statement is as follows:

        if(/$search/i)
        {
          $inloop = $search;
          my @test=split(/\|/, $_);
          $array[$a] = new $req(@test);
          $a++;
         }

Before the IF statement the variable search contains the number = 901114920
However when inside the if statement, $search loses it's value (as tested by the variable $inloop).
If I put the number 901114920 where $search is located like so:

        if(/901114920/i)
        {
          $inloop = $search;
          my @test=split(/\|/, $_);
          $array[$a] = new $req(@test);
          $a++;
         }

The statement works.

Any clues why it's not working with the variable, but is with the number. I have used a variable in this if statement and it has worked other times.

HELP ME!!!!

Thanks, Rene
0
Comment
Question by:rrivers
  • 8
  • 5
  • 5
  • +2
22 Comments
 
LVL 5

Expert Comment

by:b2pi
Comment Utility
Have you tried using perl -w and use strict?

What's setting $_?


0
 

Author Comment

by:rrivers
Comment Utility
The $_ represents the current array item. and I have tried the debugger and it comes up with no problems with the script.
0
 

Author Comment

by:rrivers
Comment Utility
$_ is being set by the reading of the file that SEARCH (not $search) represents
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
Let me restate what I think you are saying - with

if(/$search/i)
{
   $inloop = $search;

The if properly executes if $_ contains 901114920, but after the if block $inline isn't 901114920. (What is it?)

As opposed to:

if(/901114920/i)
{
   $inloop = $search;
where the if properly executes if $_ contains 901114920, and  after the if block $inline is 901114920.

Is that what you are saying?
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
$inline was a typo for $inloop, sorrry.
0
 

Author Comment

by:rrivers
Comment Utility
Let me clarify what is going on here by giving you some more code.

sub search{
 
$req=$_[1];
require ($req.".pm");
 $a=0;
 
open (SEARCH,$_[0]) || die;
 
 &lock(SEARCH);        # Lock the file so nobody else uses it.
 while(<SEARCH>)       # Loop through the file.
   {
      $_=~s/\"//g;     # Gets rid of quotes in the database.
     $outloop = $search;
     if(/$search/i){
        $inloop = $search;
        my @test=split(/\|/, $_);
        $array[$a] = new $req(@test);
        $a++;
      }
 
   } # End of While loop, so we must be at the end of the file.
 &unlock(SEARCH);
 close(SEARCH);
 return;
}

This compares $search with $_ (which is part of a line read in from SEARCH). And before you ask, the 2 searches are not getting confused because I can change the name of the variable $search to something else and it still does not work.

$outloop contains the number: 901114920 which is read in from another array. So before the if statement $search does contain the correct data.

$inloop is empty. Which means that for some reason the if statement does not like $search. however if I replace $search as so:
if(/901114920/i)
it works fine.

I would say the problem was the fact that the if statement does not like variables, however I have used this same routine with other variables and it works fine.

Trust me I used all 200 points for a reason!!!
0
 
LVL 5

Expert Comment

by:b2pi
Comment Utility
What version of perl are you using?
0
 
LVL 5

Expert Comment

by:b2pi
Comment Utility
By the way

/$variable/

_will_ interpolate.
0
 

Author Comment

by:rrivers
Comment Utility
Perl 5.004_04

By the way /$variable/  _will_ interpolate -----???????
0
 
LVL 5

Expert Comment

by:b2pi
Comment Utility
I was sort of hoping that you were going to say 5.001...

yes, /$variable/ will interpolate, at least, so says perlop

Ummm, that means that
$a = 'q';
$z = /$a/;
$x = /q/;

$z and $x will be equal.
0
 

Author Comment

by:rrivers
Comment Utility
Yes, but that still doesn't answer my question! :)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:b2pi
Comment Utility
Personally, I think something is going on here that we don't see.  Your code works here, and, I suspect, alamo feels the same way.

1.) Is the match actually succeeding? (i.e. is $inloop really being assigned when you use /$search/i  )
2.) What happens if you assign $inloop to $_ ?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Are you sure $search really contains "901114920"?
0
 

Author Comment

by:rrivers
Comment Utility
The match is succeding if I don't use the variable, but instead I use a number.
If I assign $inloop to $_ I am going to get the lines of the file it is reading.

Let me tell you exactly what is going on. I have a form. The user clicks on a radio button that has a value attached to it(that value is the number that $search becomes). A perl script then reads the input and puts it into that array (@input). @input[0] is always the number I want to do a search on. So I assign that to the variable $search. It passes it on to the search subroutine. I check at the beginning on the subroutine to make sure that $search is indeed equal to the number I want (That is $outloop). I then check to make sure that $search is equal to that same number inside the loop after the if condition is read (This is $inloop). This is where something goes wrong. $inloop loses it's value.

How ever if I hard code the number $search is equal to "901114920". the search successfully completes and gives me my correct output.

That is everything. There is nothing else going on here, no tricks. This is why I am so stumped, it makes no sense!!!!!!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is there a difference between

$search = '901114920';
if( /$search/i ){
             $inloop = $search;
             my @test=split(/\|/, $_);
             $array[$a] = new $req(@test);
             $a++;
}

and

if( /901114920/i ){
             $inloop = 901114920;
             my @test=split(/\|/, $_);
             $array[$a] = new $req(@test);
             $a++;
}
0
 

Author Comment

by:rrivers
Comment Utility
What is the difference?
Either one should correctly complete the search.
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
I can't tell precisely how you set $search, but I think there's a possibilitythat $seach looks like 901114920 to you but isn't really, i.e. there's some non-printing character there (like a terminating \n") that isn't clear to you. Therefore the search fails, and that's why $inloop is blank (it's never set).

Try checking ($search eq '901114920') at the start of the function. If it's false, that might be it.

chomp $search might also be a good idea.
0
 

Author Comment

by:rrivers
Comment Utility
Thank you so much!!!! It should of figured that it was adding extra characters at the end of the number (2 blanks in fact!). So I just used substr to get rid of them.

Doing a happy dance now!!!!

Rene

Now how do you get the point credit for this?
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
Great!

In order to receive points, one of us needs to post an answer which you can then grade. But this really should be split 3 ways, and Experts Exchange hasn't yet given you an automatic way to split points. What you should do is go to the Customer Service topic area and post a 0 point question for Linda telling her you want to split points 3 ways for this question (give this question number). She'll probably give you some extra points so that you can post dummy questions for b2pi and ozo to get them some points, and then I'll lock this one.

Glad we could help!
0
 
LVL 7

Expert Comment

by:linda101698
Comment Utility
alamo,
Post an answer to the question.  I'll add points to rrivers account so he can post questions for ozo and b2pi.  Thanks to all for your help on this question.  TEAMWORK!  GREAT JOB :-)

Linda Gardner
Customer Service @ Experts Exchange
0
 
LVL 6

Accepted Solution

by:
alamo earned 200 total points
Comment Utility
Thanks Rene, it's great when someone you help appreciates it!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Having settled the problem of what $search contains, there are a few other comments that seem worth making:
$search =~ s/\s+$//; #is an easy way to remove blanks from the end.
If $search can contain characters like '()[].*?+', it may be safer to use /\Q$search$E/
But rather than recompile the regular expression for each line of <SEARCH>
it may be better to use
 $search = lc $search;
 (index(lc($_),$search) >= 0
An easier way to append to an array would be
@array = ();
while( <SEARCH> ){
  push @array, new $req(split(/\|/);
}
If you unlock the file before closing it, someone else may lock the file before the close can flush any buffers you've writen.
It's usually better practice to let unlock be done by the close
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now