in reply to Comparing two files one with numebers and the other one with ranges range and printing matches

I stored the ranges in an array. If the number of the ranges was large, I'd probably sort them and use binary search to locate the matching one(s).
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; open my $F2, '<', 'file2' or die $!; my @ranges; while (<$F2>) { chomp; my ($version, $from, $to) = (split /,/)[ 1, 2, 3 ]; push @ranges, [ $from, $to, $version ]; } open my $F1, '<', 'file1' or die $!; while (<$F1>) { chomp; my $n = substr $_, 11, 9; my $printed; for my $r (@ranges) { if ($r->[0] <= $n && $n <= $r->[1]) { say "$_,$r->[2]"; $printed = 1; } } say "$_, no match" unless $printed; }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: Comparing two files one with numebers and the other one with ranges range and printing matches
  • Download Code

Replies are listed 'Best First'.
Re^2: Comparing two files one with numebers and the other one with ranges range and printing matches
by emadmahou (Acolyte) on Jun 13, 2015 at 02:48 UTC

    It worked perfectly

    Can you just explain to me what did you do please

    Thank you so much :),/p>

      The script first opens the second file and reads the ranges into an array of arrays. Then, it reads the first file line by line, extracts the number from the given position, and iterates over all the ranges to find the matching one(s). If it finds them, it prints the desired output, if not, it prints "no match".
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; unlink $outputfile; unlink $outputfile2; unlink $outputfile3; sub trimspaces { my @argsarray = @_; $argsarray[0] =~ s/^\s+//; $argsarray[0] =~ s/\s+$//; return $argsarray[0]; } open(INPUT , "< D:\\Home\\test\\imbfilelist.txt") or die $!; open(INPUT2 , "< D:\\Home\\test\\imbrange.txt") or die $!; my $n; my $value; my @ranges; my $isMatch; my $printed; my $fVersion; my %versionHash=(); while (<INPUT2>) { chomp; my ($version, $from, $to) = (split /,/)[ 1, 2, 3 ]; push @ranges, [ $from, $to, trimspaces($version)]; if (!exists $versionHash{trimspaces($version)}) { $versionHash{trimspaces($version)}=0; } } $versionHash{"No Matched"}=0; # foreach my $key (keys %fileHandleHash) # { # close $fileHandleHash{$fVersion}; # } close INPUT2; while (<INPUT>) { $isMatch=0; $n = substr($_,12-1,9); for my $r (@ranges) { if ( $n >= $r->[0] && $n <= $r->[1]) { $fVersion=$r->[2]; if (exists $versionHash{$fVersion}) { $versionHash{$fVersion}++; } $isMatch=1; last; } } if (!$isMatch) { $versionHash{"No Matched"}++; } } foreach my $key (keys %versionHash) { print STDOUT "$key IMB Count: " . $versionHash{$key} . "\n"; } close INPUT;
        I have change the code to loop throw my output looks like this:
      • folded IMB Count: 15
      • No Matched IMB Count: 1
      • selfmail IMB Count: 14
      • the range comes from here
      • imb,folded ,655575645,827544086
      • imb,selfmail ,827549192,827572977
      • and still the same list for input I now need to be able to create files for each instance: first file will be folded.txt with 15 of the original number seconde file will be nomatch.txt with the original as well and same for the third. any advice??