Ltaylor
Ltaylor

Reputation: 85

print each range in perl array

I have an array of ranges in Perl and need a way to loop through each range in the array, search a number and print the min..max indexes for each range. I am able to do this in bash shell scripting but having some trouble in Perl.

My code:

#!/usr/bin/perl 
use List::Util qw(max min);   

$search_num = 95;

@ranges = (73..80, 92..107, 941..1000, 3000..3170);

foreach $num (@ranges) {
    $range_min = min(@ranges);
    $range_max = max(@ranges);
    if ($search_num == $n) {
        print "$search was found in range $range_min..$range_max\n";
    }
} 

Desired output:

95 was found in range 92..107

The following works fine for indicating per hard coded range but need a way to have a series of ranges in an array to loop, search and display where found. The following works:

@range = (92..107);

foreach $num (@range) {
    $range_min = min(@range);
    $range_max = max(@range);

    if ($search_num == $num){
        print "$search_num was found in range $range_min..$range_max\n";
    }
}

Output:

95 was found in range 92..107

thanks for any advice.

Upvotes: 0

Views: 385

Answers (1)

Sinan Ünür
Sinan Ünür

Reputation: 118128

@ranges=(73..80, 92..107, 941..1000, 3000..3170);

You seem to be under the impression that this will put separate range objects in @ranges. Instead, @range contains the following flat list:

$ perl -E '@ranges=(73..80, 92..107, 941..1000, 3000..3170); say "@ranges"'
73 74 75 76 77 78 79 80 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170

You can insert references to anonymous arrays in @ranges:

@ranges = ([73..80], [92..107], [941..1000], [3000..3170]);

However, since you already know the upper and lower limits of each range, why are you wasting memory?

@ranges=([73, 80], [92, 107], [941, 1000], [3000, 3170]);

Here is one way to implement that:

#!/usr/bin/env perl

use strict;
use warnings;

my @ranges=([73, 80], [92, 107], [941, 1000], [3000, 3170]);
my $search = 95;

my $found = search_in_ranges($search, \@ranges);

for my $r ( @$found ) {
    printf "%d was found in [%d, %d]\n", $search, $r->[0], $r->[1];
}

sub search_in_ranges {
    my ($n, $ranges) = @_;
    return [ grep $n >= $_->[0] && $n <= $_->[1], @$ranges ];
}

See also perldoc perlreftut which is installed along with your Perl distribution.

Upvotes: 1

Related Questions