hellohello1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a question regarding about hash. I have two text files, userinput.txt (USERINPUT) and database.txt (CURINFILE). In the userinput.txt, it has all the values the user want to search using that:
e.g. Mass Time 100.20 0.55 150.34 0.23 223.45 1.22 223.44
sometimes time value is not known so it will be remained blank. The database.txt consists of a library that the user want to search through using the values from userinput.txt.
database.txt: Mass Time Name Type 223.41 1.20 John Boy 111.23 5.21 Betty Girl 100.25 0.56 Ken Boy . . .
So the idea is that after searching the database, it will return the values (print out in new textfile) of John and Ken if both the user inputs and library match (there is tolerance set by user to allow search to be less rigid) based on the mass and time or mass alone (if no time given). User can input the tolerance of the mass and time (range).

$masstoleranceinput and $rttoleranceinput refer to the user input (allow them to give +/- (range)) to be less rigid in the search.

e.g. for the above userinput, it will return this values in new textfi +le Mass Time Name Type 100.20 0.55 Ken Boy 223.45 1.22 John Boy 223.44 John Boy

So what I have here in my code is:

#### Put User input values into hashes in order to search database### open (CURINFILE,'database.txt') or die $!; while (<CURINFILE>) { open (INPUTFILE,'userinput.txt') or die $!; while (my $line=<INPUTFILE>) { chomp($line); (my $massinput,my $rtinput) = split /\t/, $line; $hash{$massinput} = $rtinput; } use Data::Dumper; print %hash; #test if userinput values are stored ####### Specify tolerance (user input) ############### print "\n Set desired tolerance\n "; print "-------------------------- "; print "\n Mass tolerance: "; $masstoleranceinput = <STDIN>; chomp $masstoleranceinput; print " Time tolerance: "; $rttoleranceinput = <STDIN>; chomp $rttoleranceinput; ######### Start Search Database ############ push @full_data , [split /\t/] while (<CURINFILE>); open OUT1, ">$path/Results.txt" or die "error couldn't open output f +ile"; print OUT1 "Mass\tTime(Min)\tName\tType\n"; for $arr_ref1(@full_data) { for my $index1 (1) #mass { for my $index3(2) #Time { for my $index2 (0) #Name { for my $index4 (3) #Type { $name1 =$$arr_ref1[$index2]; $mode = $$arr_ref1[$index4]; # Mass (+/- tolerance set by user) $mass1 = $$arr_ref1[$index1]; $masst1 = $$arr_ref1[$index1] + $masstoleranceinput; $masst2 = $$arr_ref1[$index1] - $masstoleranceinput; # Time (+/- tolerance set by user) $rt = $$arr_ref1[$index3]; $rt1 = $$arr_ref1[$index3] + $rttoleranceinput; $rt2 = $$arr_ref1[$index3] - $rttoleranceinput; # Condition #stuck here if (( $masst2 <= $massinput && $massinput <= $masst1 ) && ( $rt2 <= + $rtinput && $rtinput <= $rt1 )) { my $result1 = "$mass1\t$rt\t$name1\t$mode"; print OUT1 $result1; } #end of if loop else{ if (( $masst2 <= $massinput && $massinput <= $masst1 )) { my $result2 = "$mass1\t$rt\t$name1\t$mode"; print OUT1 $result2; } #end of if loop } } # End of while loop
Everything works ok until the condition (criteria). I am kind of stuck at the condition part. Is there a way to set a condition for the hash? Or is my coding wrong? I have tried this initially for the upper part of the code:
for my $array_input(@userinputdata) { $massinput = $$array_input[0]; $rtinput = $$array_input[1]; }
But it will only return the last line of the search during the condition instead of everything. So I was wondering to use hash? I have read up in internet on hashes but still could not understand how I can use that to search database.

Pardon for the long post. I appreciate for some help/advice.

Thanks! :)

Replies are listed 'Best First'.
Re: Search based on user input and return values (more subs)
by Anonymous Monk on Aug 21, 2014 at 07:26 UTC

    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__
      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