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

I have an interface in a router, where i need to add multiple services. Ex. ge-3/1/0 can have sub-interfaces from ge-3/1/0.1 to ge-3/1/0.5000. I have 3 services, say x, y z, . X should get values from 1 to $max_x, y should get from $max_x+1 to $max_y and z should be from $max_y+1 to $maxz. Values of X, Y, Z will be specified by the user , which is in a different file . I have used the following logic

if (defined (x) || defined(y) || defined(z) { for ($i = 1; $i<=$max_x ; $i++) { $router cmd ( set interface ge-3/1/0.$i ) if ($i == $max_x) { break; } if (defined(y) { for ($i = $max_x+1; $i<=$max_y ; $i++) { $router cmd ( set interface ge-3/1/0.$i ) if ($i == $max_y) { break; } }}

Code is not assigning correct values . anything missing here

Replies are listed 'Best First'.
Re: Identify values from variables
by kroach (Pilgrim) on Dec 26, 2015 at 18:22 UTC
    This code is full of basic errors (no sigils, bad use of break, using "set interface" with barewords). Make sure this really is Perl and if yes, see perlintro.
Re: Identify values from variables
by Athanasius (Archbishop) on Dec 27, 2015 at 03:34 UTC

    The requirements are not entirely clear, and you don’t explain how the logic is failing. Taking the specification together with the pseudocode shown, the implementation in Perl could be as simple as the following (skeleton code, untested):

    #! perl use strict; use warnings; my $file = 'config.txt'; my $prefix = 'ge-3/1/0.'; my ($router, $max_x, $max_y, $max_z) = get_values($file); my $max = $max_z // $max_y // $max_x // 0; set_interface($router, $prefix . $_) for 1 .. $max; sub get_values { ... } sub set_interface { ... }

    The key line here is my $max = $max_z // $max_y // $max_x // 0; which uses the logical defined-or operator.

    If this logic doesn’t meet your needs (and I doubt that it will), it may at least help you to clarify the requirements by highlighting the way(s) in which it falls short.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,