• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Perl IF statement

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
rrivers
Asked:
rrivers
  • 8
  • 5
  • 5
  • +2
1 Solution
 
b2piCommented:
Have you tried using perl -w and use strict?

What's setting $_?


0
 
rriversAuthor Commented:
The $_ represents the current array item. and I have tried the debugger and it comes up with no problems with the script.
0
 
rriversAuthor Commented:
$_ is being set by the reading of the file that SEARCH (not $search) represents
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
alamoCommented:
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
 
alamoCommented:
$inline was a typo for $inloop, sorrry.
0
 
rriversAuthor Commented:
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
 
b2piCommented:
What version of perl are you using?
0
 
b2piCommented:
By the way

/$variable/

_will_ interpolate.
0
 
rriversAuthor Commented:
Perl 5.004_04

By the way /$variable/  _will_ interpolate -----???????
0
 
b2piCommented:
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
 
rriversAuthor Commented:
Yes, but that still doesn't answer my question! :)
0
 
b2piCommented:
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
 
ozoCommented:
Are you sure $search really contains "901114920"?
0
 
rriversAuthor Commented:
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
 
ozoCommented:
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
 
rriversAuthor Commented:
What is the difference?
Either one should correctly complete the search.
0
 
alamoCommented:
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
 
rriversAuthor Commented:
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
 
alamoCommented:
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
 
linda101698Commented:
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
 
alamoCommented:
Thanks Rene, it's great when someone you help appreciates it!
0
 
ozoCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 8
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now