Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Range Operator in If Statement

by mmartin (Monk)
on Jan 05, 2012 at 15:14 UTC ( #946417=note: print w/ replies, xml ) Need Help??


in reply to Range Operator in If Statement

Wow, thanks everybody, what a response..!!

kejohm,
In terms of updating to Perl 5.10 and using the Smart Match Operator. I did look into that but updating the Perl version wasn't really an option at the moment bc it is a production server and a bunch of things running depend on that. But I did want to try to use that at some point...


ww,
Thanks for the reply, The example you show is sort of what I was trying to achieve with the ranges, maybe I'll give that one a try as well. Thanks again!


LanX,
Oh ok I gotcha... Bc it was working just not in the way I thought it would...? Thanks for the info.


NetWallah,
Hey, that looks cool I'll have to look into that Module as well. Seems alot like the Acme::Tools Module that I eventually went with from toolic's suggestion... Thanks I'll have to download that Module.


TJPride,
That's a good one..! I like that! Don't know why I didn't think of that. Clean and simple, since the control structure, once it finds a match, it will drop out of the if/elsif clause which leads to not having to use a range to test on, but just one test instead... Cool thanks..!


GrandFather,
Hey thanks for the reply. Sorry it took me a while to respond back but I decided to go with toolic's suggestion with the ACME::Tools solution... Works Good!
But in terms of a backstory. The thing I need this for is a script to run along side with MRTG and Interafce Templates to check which GigabitEthernet port it is and then separate them into groups to test for Backplane usage on a Cisco switch. Backplane is Bundeled ports (usually 8 consecutive one's) ranging from (Gi3/1<-->Gi3/48; Gi4/1<-->Gi4/48; ... all the way to ... Gi7/1<-->Gi7/48).

So basically some of the groups would be in ranges as follows... For example the range Gi3/1..Gi3/8 would be "Bundle-Gi3A", and Gi3/9..Gi3/16 would be "Bundle-Gi3B" all the way up to Bundle-Gi3F. As for Gi4 -to- Gi7 it would be the same Letters only difference would be the Gi number...


Here is my entire solution to the original question:
The original question had to do with the Subroutine "get_bundle()"
I know it's a SIMPLE program but here it is. Basically you just pass it the interface name (i.e. "Gi3/7") broken into two pieces, (1) the Gi number "Gi3" and (2) the reference number "7", which is passed from an MRTG Template/Script and then passed back to the Template to be added to the config ("cfg") file.

#!/usr/bin/perl use warnings; use strict; use Getopt::Long; use Acme::Tools qw(between); # Declare Command Line Option's Variables my $Gi_number; my $ref_number; my $get_what; # Declare regular variables my $bundle; my $target_lines; #my $target_name = "target_name"; my @number; my $GInum; my $userGraph; # Process Command-Line Options with the GetOptions::Long Module if ( @ARGV > 0 ) { GetOptions('Gi_number=s' => \$Gi_number, 'ref_number=s' => \$ref_number); } ### Extract the 'Gi' Number (i.e. Gi4 --> '4') and set it to $GInum @number = ($Gi_number =~ /(\d)/); $GInum = $number[0]; &get_bundle(); ### Build the the UserDefined Graph Name for the routers.cgi*Graph[] L +ine $userGraph = "Bundle-$Gi_number$bundle"; if ($bundle) { $target_lines .= "routers.cgi*Graph[\$target_name]: $userGraph tot +al average active\n"; } else { print "WARNING: No Bundle Letter has been assigned to \$bundle +... Try again.\n"; exit 2; } ###################################################################### +############## # SUB - get_bundle() --- This sub will take the interface reference nu +mber Gi3/"7" # # which is the '7' and get the corresponding Bundle letter +s.# ###################################################################### +############## sub get_bundle() { if (between($ref_number, 1, 8)) { $bundle = "A"; } if (between($ref_number, 9, 16)) { $bundle = "B"; } if (between($ref_number, 17, 24)) { $bundle = "C"; } if (between($ref_number, 25, 32)) { $bundle = "D"; } if (between($ref_number, 33, 40)) { $bundle = "E"; } if (between($ref_number, 41, 48)) { $bundle = "F"; } } ###################################################################### +########### ### END: get_bundle() print "$target_lines";

I'm SURE there is a 100 different ways to do this, but this works for my current situation...

I REALLLY want to thank all of you for your suggestions, it was MUCH MUCH more then I expected..!!

Thanks Again Everybody,
Matt



Comment on Re: Range Operator in If Statement
Download Code
Re^2: Range Operator in If Statement
by Not_a_Number (Parson) on Jan 05, 2012 at 16:00 UTC
    I'm SURE there is a 100 different ways to do this...

    Here's one way to assign to $bundle that requires a lot less typing. Replace your call to get_bundle and the sub itself with:

    $bundle = [ 'A' .. 'F' ]->[ ( $ref_number - 1 ) / 8 ];

    Update: Removed unnecessary parentheses around the arrayref.

      Not_a_Number, thanks for the reply...

      Cool stuff... Could you explain a little how that works exactly? I'm still relatively new to Perl.


      Thanks,
      Matt


        Could you explain...

        Let me try. First,

        [ 'A' .. 'F' ]

        is a reference to an anonymous arrray (see references quick reference). To access, for example, the second element of this array, the syntax is:

        [ 'A' .. 'F' ]->[1]

        (You could also do ${ [ 'A' .. 'F' ] }[1] but I find the first form cleaner.)

        Then, instead of hard-coding the array index, my snippet calculates it in the fashion indicated in LanX's post above. The only difference is that I've removed int() since perl happily does this part of the job for me when I use a floating point number as an array index.

        To clarify, the following code, using a named array and an intermediate variable ($index), does the same thing:

        my @array = ( 'A' .. 'F' ); my $ref_number = 42; # or whatever my $index = ( $ref_number - 1 ) / 8; my $bundle = $array[ $index ]; print $bundle;

        hth, dave

Re^2: Range Operator in If Statement
by LanX (Canon) on Jan 05, 2012 at 22:51 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946417]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-08-21 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls