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

Is there an alternative way of handling those nested loops (for beginners) so that I can print everything out in one go so that there wont be any duplicate print after going through loop?

Here is a parody

Q: How do I cut down this tree?
A: Use a saw
Q: Is there an alternative way?
A: Beaver
A: On second thought seven quad bladed axes work best

Seriously though, see Re: Search based on user input and return values (more subs) and do those things, use that program, improve that program, when you're stuck, post your progress

  • Comment on Re^3: Search based on user input and return values (more subs)

Replies are listed 'Best First'.
Re^4: Search based on user input and return values (more subs)
by hellohello1 (Sexton) on Oct 15, 2014 at 09:39 UTC
    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?

        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!