Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^3: array or array ref as variable

by HelenCr (Monk)
on Jun 28, 2013 at 07:32 UTC ( #1041163=note: print w/replies, xml ) Need Help??

in reply to Re^2: array or array ref as variable
in thread array or array ref as variable

( Note the update:  @{$arr_hash{$_}} = collectData($_, $CFG{$CFG{ uc $_}});)

I reckon the calling program can go like this:

my (@fw, @vrf, @vlan, @portchannel); my %arr_hash = (fw=> \@fw, vrf=> \@vrf, vlan =>\@vlan, portchannel => +\@portchannel); foreach (keys %arr_hash) { @{$arr_hash{$_}} = collectData($_, $CFG{$CFG{ uc $_}}); }
(not tested)


Replies are listed 'Best First'.
Re^4: array or array ref as variable
by Hossein (Acolyte) on Jun 28, 2013 at 08:12 UTC
    It works just fine :) Thank you Helen :)

    what about a smarter way of doing this part?

    $str =~ /^fw/ ? { @array = NT::FW->extract($content, %CFG) +} : $str =~ /^vrf/ ? { @array = NT::VRF->extract($content, %CFG) + } : $str =~ /^vlan/ ? { @array = NT::VLAN->extract($content, %CFG +) } : $str =~ /^portchannel/ ? { @array = NT::PORTCHANNEL->extract($conten +t, %CFG) } : return;
      In my opinion, something like this could work ( dropping the sub, in case you don't need that sub for something else):

      my (@fw, @vrf, @vlan, @portchannel); my @arr_methods = ([ \@fw, &NT::FW], [\@vrf, &NT::VRF], [\@vlan, &NT:: +VLAN], [ \@portchannel, &NT::PORTCHANNEL] ); foreach (@arr_methods) { @{$_->[0]} = ($_->[1])->extract($content, %CFG); }

      I haven't tested it, the right hand-side of the assignment may not work, (but it's close), if it doesn't work, maybe one of the higher gurus here can fix it.

      Run it and tell us the result


        ok, I did this:
        my @arr_methods = ([ \@fw, \&NT::FW, 'FW'], [\@vrf, \&NT::VRF, 'VRF' +], [\@vlan, \&NT::VLAN, 'VLAN'], [ \@portchannel, \&NT::PORTCHANNEL, +'PORTCHANNEL'] ); foreach (@arr_methods) { @{$_->[0]} = ($_->[1])->extract( &slurpFile($CFG{$CFG{$_->[2]} +} ), %CFG); }

        If I run  [ \@fw, &NT::FW, 'FW'] I'll get this error message:

        Undefined subroutine &NT::FW called at line 32.

        but when I do this: [ \@fw, \&NT::FW, 'FW'] that error message is gone.

        and when I run it I get this error message:

        Can't call method "extract" on unblessed reference at line 48.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041163]
[Discipulus]: good first day of Spring nuns and monks
[LanX]: yeah let's break the ice!

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2018-03-21 07:57 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (264 votes). Check out past polls.