Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

limits

by harry34 (Sexton)
on Nov 28, 2002 at 10:37 UTC ( [id://216275]=perlquestion: print w/replies, xml ) Need Help??

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

If I have an @array containing two columns of numbers.

I can use: if (($column_1[$i] gt 10 && lt 50) and ($column_2[$i] gt 100 && lt 200)){
$number[$i] = "$column_1[$i]   $column_2[$i].....good  numbers\n";
foreach $_(@number) { print TEXT "$_"; }

How would I code to say numbers just outside the specified range (e.g. numbers around 8-10 and 50-52 for column_1 and 98-100 and 200-202 for column_2) are possibly OK and everything else above and below that range is crap.

Is there an alternative to using lt and gt ?

cheers for your wisdom.
harry34

Replies are listed 'Best First'.
Re: limits
by Abigail-II (Bishop) on Nov 28, 2002 at 11:10 UTC
    Well, unless you have defined your own lt function, you cannot use if (($column_1[$i] gt 10 && lt 50) and ($column_2[$i] gt 100 && lt 200)). It's a syntax error - lt is a binary operator.

    A few points, if you know how to test whether something is in the range, you know how to test whether something is outside of the range. Either you are inside, or outside. So to test whether something is outside of the range, just test whether it's not inside the range.

    Also, if you are doing numerical compares, you should use the numerical comparison operators: <, <=, ==, >=, > and <=>. gt and lt and friends are for alphabetic compares.

    Abigail

Re: limits
by jreades (Friar) on Nov 28, 2002 at 10:45 UTC

    Uh, you could use the numerical comparison... why are you using the ASCII comparison?

    I think that there are some conceptual problems here that the following code might help:

    my @number = (); for (my $i = 0; $i < @column_1; $i++) { if (($column_1[$i] >= 8 && $column_1[$i] <= 52) && ($column_2[$i] >= 98 && $column_2[$i] <= 202)) { push @number, join(' ', $column_1[$i], $column_2[$i]); } } foreach (@number) { print $_ . "\n"; }

    If you're looking for something more subtle where the fuzziness of the match is unclear then you'd have to improve this approach substantially.

    HTH

Re: limits
by cLive ;-) (Prior) on Nov 28, 2002 at 16:45 UTC
    As well as taking on board other comments, you may want to change your approach to something like this:
    #!/usr/bin/perl -w use strict; # sample data my @column_1 = qw(13 14 10 34 73 64 48 52 51 74 8 85 36 74 34 53 9 86 + 59 15 25); my @column_2 = qw(3 74 98 185 36 74 34 201 99 86 59 15 25 123 134 16 +202 73 64 48 52); # "good" numbers my %col_1_good = map { $_, 1 } (11..49); my %col_2_good = map { $_, 1 } (101..199); # "fair" numbers my %col_1_fair = map { $_, 1 } (8..10, 50..52); my %col_2_fair = map { $_, 1 } (98..100, 200..202); my @good_pairs = (); my @fair_pairs = (); for my $i (0..$#column_1) { if ( $col_1_good { $column_1[$i] } && $col_2_good { $column_2[$i] +} ) { push @good_pairs, "$column_1[$i] $column_2[$i]"; } elsif ( $col_1_fair { $column_1[$i] } && $col_2_fair { $column_2[$ +i] } ) { push @fair_pairs, "$column_1[$i] $column_2[$i]"; } } print join "\n", "Good Numbers:", @good_pairs, "\nFair Numbers:", @fai +r_pairs;

    cLive ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2024-04-18 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found