This is similar to Anonymous Monk's suggestion but builds a lookup table. Tables can often simplify your logic and avoid a string of if blocks.
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 2; my @ranges = ( # low high conf_file [ 204, 381, 2], [ 22, 199, 4], [ 513, 690, 6], [ 823, 1000, 8], [1067, 1244, 10], [1311, 1488, 12], [1555, 1732, 14], [1799, 1976, 16], [2043, 2220, 18], [2287, 2464, 20], ); my %lookup; for my $range (@ranges){ my ($low, $high, $slot) = @{$range}; for my $index ($low..$high){ $lookup{$index} = $slot; } } #print Dumper \%lookup; #exit; my @ifindex = (204, 22, 513); my ($msan_slot, $msan_port) = translate_ifindex(@ifindex); sub translate_ifindex { my @ifindex = @_; my $ports_ini_dir = "/home/portal/bin/conf/snmp_set"; my (@msan_slot, @msan_port); for my $t (0..$#ifindex) { if (exists $lookup{$t}){ my $cnf_file = sprintf(q{%s/snmp_ports_slot%02d.txt}, $ports_ini +_dir, $lookup{$t}); my $cfg = Config::IniFiles->new( -file => $cnf_file) or die q{cant open *$cnf_file*}; my $slot = $cfg->val($ifindex[$t], "slot"); my $port = $cfg->val($ifindex[$t], "port"); push @msan_slot, $slot; push @msan_port, $port; } } return \@msan_slot, \@msan_port; }
Untested due to not having the config files.

updated: corrected table syntax and sprintf format string


In reply to Re: Undefined value by wfsp
in thread Undefined value by Ravendark

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.