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! :)

In reply to Search based on user input and return values by hellohello1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.