Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: array or array ref as variable

by Hossein (Acolyte)
on Jun 28, 2013 at 07:09 UTC ( #1041160=note: print w/ replies, xml ) Need Help??


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

This is my call

my (@fw, @vrf, @vlan, @portchannel); @fw = &collectData('fw', $CFG{$CFG{FW}}); @vrf = &collectData('vrf', $CFG{$CFG{VRF}}); @vlan = &collectData('vlan', $CFG{$CFG{VLAN}}); @portchannel = &collectData('portchannel', $CFG{$CFG{PORTCHANNEL}});

and this is the sub

sub collectData { my $str = shift; my $type = shift; my @array; my $content = &slurpFile($type); if ($CFG{READFILE} eq $CFG{ERROR}){ print $CFG{ERROR_OPEN} = s/--file--/$type/ ,"\n"; return; } $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; return @array; }

How can I do this with less lines of code?


Comment on Re^2: array or array ref as variable
Select or Download Code
Re^3: array or array ref as variable
by HelenCr (Monk) on Jun 28, 2013 at 07:32 UTC
    ( 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)

    Helen

      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;
      /Hossein
        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

        Helen

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-08-29 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (287 votes), past polls