in reply to simple search and print

dr_jgbn,
For example, grab 85.11, it is within 0.5 of 85.4

Last time I checked, 85.4 + .5 is 85.9 which is still less than 85.11. I will assume for now that was just a typo.

Since you do not say how large these files are, I will assume memory is no object. Since you do not say if the tab delimited file has more than two fields, I will assume that the name is in the first field and the second field is the number. Finally, I will assume run-time speed is not critical.

#!/usr/bin/perl use strict; use warnings; open (NUMBERS, '<', "numbers.txt") or die "Unable to open numbers.txt +for reading : $!"; open (NAMES, '<', "names.txt") or die "Unable to open names.txt for re +ading : $!"; my %numbers = map {chomp; $_ => 1} <NUMBERS>; my %names = map {chomp; split /\t/ , $_ , 2} <NAMES>; for my $name ( keys %names ) { my $names_number = $names{$name}; for my $number ( keys %numbers ) { my $delta = abs $number - $names_number; if ( $delta <= .5 ) { print "$name : $names_number is within range of $number\n" +; last; } } }
Cheers - L~R

Replies are listed 'Best First'.
Re: Re: simple search and print
by pzbagel (Chaplain) on Feb 24, 2004 at 23:39 UTC

    Uhh, 85.11 is less than 85.9 or even 85.4. However 85.4 - .5 = 84.9 so 85.11 does fall with +/- .5 of 85.4.

    Decimals not version numbers.

    Later

      pzbagel,
      Well isn't a good thing that I went ahead and wrote the code anyway? For some reason my brain was not processing the decimal place correctly. It was seeing 4 + 5 = 9 < 11 so wrong. The code should however work regardless of my confusion.

      Thanks - L~R

Re: Re: simple search and print
by fizbin (Chaplain) on Feb 25, 2004 at 08:42 UTC
    I'll note that in what you posted above there's no reason to read NAMES into memory - in fact, if the order of names in NAMES is significant, what you do will cause a problem.

    When I read the post, my impression was that the order of values in NUMBERS was what the poster wanted preserved, and so would have said:

    #!/usr/bin/perl use strict; use warnings; open (NUMBERS, '<', "numbers.txt") or die "Unable to open numbers.txt +for reading : $!"; open (NAMES, '<', "names.txt") or die "Unable to open names.txt for re +ading : $!"; my @namevals = map {chomp; [split /\t/ , $_ , 2]} <NAMES>; while (<NUMBERS>) { s/\s//gs; my $current = $_ + 0; map {print $_->[0]," matched $current\n" if (abs($_[1] - $current) <= 0.5);} @namevals; }
    If runtime is an issue, namevals could always be split into a hash of lists based on the floor() of the given value - then, when comparing, you'd only compare against the two lists that contained namevals near the target value.