in reply to Search based on user input and return values

Everything works ok until ... Or is my coding wrong? ... I appreciate for some help/advice.

*cough* subs , more subs, not too many nesting loops, code that compiles, minimal code focusing on the problem , code that doesn't prompt, no weirdo looping constructs, seperate math from mechanics (more subs) ....

same advice How to store the output from foreach loop into variable/array without printing?

If you make named subroutines you can debug the smallest piece(sub) possible, not the whole thing at once in a blender

#!/usr/bin/perl -- ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while " -otr + -opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; my $massToll = 1; my $timeToll = 2; my @massTime = ( [ 'Mass', 'Time', ], [ '100.20', '0.55', ], [ '150.34', '0.23', ], [ '223.45', '1.22', ], [ '223.44', ], ); my @database = ( [ 'Mass', 'Time', 'Name', 'Type', ], [ '223.41', '1.20', 'John', 'Boy', ], [ '111.23', '5.21', 'Betty', 'Girl', ], [ '100.25', '0.56', 'Ken', 'Boy', ], ); #~ shift @massTime; UnstuckedSmartWay( \@database, $massTime[1], $massToll, $timeToll ); exit( 0 ); sub UnstuckedSmartWayOfSolvingProblems { my( $db, $mtref, $massToll, $timeToll ) = @_; my( $mass, $time ) = @$mtref; for my $row ( @$db ){ my $ret = NaturalIntuitive( $row, $mass, $time, $massToll, $ti +meToll ); } } ## end sub UnstuckedSmartWay sub NaturalIntuitive { my( $row, $mass, $time, $massToll, $timeToll ) = @_; my( $rowMass, $rowTime, $rowName, $rowType ) = @$row; my $math = ...; return $math; } ## end sub NaturalIntuitive __END__

Replies are listed 'Best First'.
Re^2: Search based on user input and return values (more subs)
by hellohello1 (Sexton) on Sep 26, 2014 at 03:17 UTC
    Thank you :) Manage to work things out and things are working great so far.

    I know my code isn't a good way for coding since I'm still learning at beginner stage. I agree that it has too many nested for loop which is giving me a headache sometimes because it is printing out duplicate results due to the while and for loop. I can't print outside nested loops as it will give me only the last line as output.

    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?

    I created the nested for loops in attempt to get the values from the columns in the textfile.

      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

        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?