hellohello1 has asked for the wisdom of the Perl Monks concerning the following question:
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.e.g. Mass Time 100.20 0.55 150.34 0.23 223.45 1.22 223.44
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).database.txt: Mass Time Name Type 223.41 1.20 John Boy 111.23 5.21 Betty Girl 100.25 0.56 Ken Boy . . .
$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:
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:#### 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
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.for my $array_input(@userinputdata) { $massinput = $$array_input[0]; $rtinput = $$array_input[1]; }
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 | |
by hellohello1 (Sexton) on Sep 26, 2014 at 03:17 UTC | |
by Anonymous Monk on Sep 26, 2014 at 10:08 UTC | |
by hellohello1 (Sexton) on Oct 15, 2014 at 09:39 UTC | |
by Anonymous Monk on Oct 16, 2014 at 21:38 UTC | |
|