in reply to Re^3: Search based on user input and return values (more subs)
in thread Search based on user input and return values

Hi,

I have manage to improve the program and reduce the weird loopings based on your advice. I am kind of stuck at the if condition part, in which I am unsure if it is the correct way to search for the data. I hope I am in the correct direction.

while (<LIBRARYDATA>) { #add the name, mass, rt(time) and type to an array chomp $_; my @columns = split (/\t/, $_); my $keyname = $columns[0]; my $keymass = $columns[1]; my $keytime = $columns[2]; my $keytype = $columns[3]; my $key = "$keymass"."\t"."$keytime"."\t"."$keyname"."\t"."$keytyp +e"."\n"; # Mass (to allow range for user to search based on tolerance they +input) $masst1 = $keymass + $masstoleranceinput; $masst2 = $keymass - $masstoleranceinput; for ($keymass) { for ($keytime) { if (defined ($keytime) && defined ($rtinput)) #skip those with bl +ank cells at "Time" column { #allow user to choose time range based on tolerance they set $rt1 = $keytime + $rttoleranceinput; $rt2 = $keytime - $rttoleranceinput; #match according to mass and time against library if (( $masst2 <= $massinput && $massinput <= $masst1 ) && ( $r +t2 <= $rtinput && $rtinput <= $rt1 )) { print OUT1 "$massinput\t$rtinput\t\t$key"; } } else #for those cells at "Time" column that is left blank, go +to this loop where only mass is being matched { #match against mass only if (( $masst2 <= $massinput && $massinput <= $masst1 )) { print OUT1 "$massinput\t$rtinput\t\t$key"; } } } } }

rtinput and massinput is key in by user, in which sometimes the rtinput will be leave blank. $masstoleranceinput and $rttoleranceinput is also by user input.

LIBRARYDATA refer to the data that I want to match the user inputs with. Some values under "Time" will be blank.

_LIBRARYDATA_ Name Mass Time Type John 223.41 1.20 Boy Betty 111.23 Girl Ken 100.25 0.56 Boy . . . _INPUTDATA_ e.g. Mass Time 100.20 0.55 150.34 0.23 223.45 1.22 223.44

The problem I have here is that there will be error once it pass through the first if loop where the error stated that it is unable to do calculation if the value is left blank. Since I have add in that if the values is defined, then continue, otherwise, skip to the next if condition which is just to compare the mass alone.

So the output I get is that I am only able to print those that contain all values. Those lines that has the time (left empty) could not be printed out.

So did I miss out something which cause the error and prevent those (with the value of "Time" left blank) from printing out?

Replies are listed 'Best First'.
Re^5: Search based on user input and return values (allergic to subroutines)
by Anonymous Monk on Oct 16, 2014 at 21:38 UTC
      Hi, I'm sorry. I'll try again! I'm not that allergic to subroutines, I am still working on it and trying to learn how to sub, but for my previous question, I was trying to ask if I am using "defined" the correct way to ensure that the blank values can be skipped. Thanks for the first link to the thread. Will read up and see how it goes. My apologies for causing you some annoyance!

        I was trying to ask if I am using "defined" the correct way to ensure that the blank values can be skipped.

        Does it work? It looks like it would work to skip blank lines

        small style tip, we usually don't write defined($foo) just  defined $foo