Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comparing

by DS (Acolyte)
on Jul 22, 2002 at 04:39 UTC ( [id://183941]=perlquestion: print w/replies, xml ) Need Help??

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

do you know a right way of doing this : I have file one with this info
22-28 36 44-49 234 77 26
and file two :
23 11 21 44 233 26
I need to see if any number or numbers in file one matches a number in file two or if there is number in file two that is in the range of a number in file one . If there is , then print it ... for example ,, in my files I would print 26 since it appears in both file and 23 since it is in the range of 22-28 I am doing somthing like this but don't seem to get the ones in the range.
for (@fileTow) { for (@fileOne) { if ($_ =~ /(\d+)-(\d+)/) { my ($start, $end) = split(/-/, $_)}; if (($fileTowValue== $_ ) or ( $fileTowValue>= $start) and ( $fileTowV +alue<= $end)) {print"$fileTowValue\n"}; } }
thanks for any help or adivce on doing this ..

Replies are listed 'Best First'.
Re: comparing
by gav^ (Curate) on Jul 22, 2002 at 04:49 UTC
    I'm not sure what on earth you're doing in your code, you have 2 loops using $_ and you pull a magic $fileTowValue out of thin air.

    Here is something that works:

    my @file1 = qw(22-28 36 44-49 234 77 26); my @file2 = qw(23 11 21 44 233 26); my %wanted = (); foreach my $num (@file1) { if ($num =~ /^(\d+)-(\d+)$/) { $wanted{$_}++ for $1 .. $2; } else { $wanted{$num}++; } } foreach (@file2) { print $_, "\n" if $wanted{$_}; } __END__ Output: 23 44 26

    gav^

      thanks :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183941]
Approved by gav^
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (1)
As of 2024-04-25 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found