Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: array or array ref as variable

by GrandFather (Sage)
on Jun 28, 2013 at 06:20 UTC ( #1041152=note: print w/ replies, xml ) Need Help??


in reply to array or array ref as variable

You seem to have almost answered your own question in the asking of it, so maybe a different question is in order? What are you trying to achieve? That is, what is the big picture objective? Maybe you'd be better using a hash of arrays or, more likely, an array of hashes, or even an array of objects to manage your data?

my @everything = (\@fw, \@vlan, \@vrf, \@more); for my $arrayRef (@everything) { theSub(@$arrayRef); }

seems to answer your immediate issue.

True laziness is hard work


Comment on Re: array or array ref as variable
Download Code
Replies are listed 'Best First'.
Re^2: array or array ref as variable
by HelenCr (Monk) on Jun 28, 2013 at 07:00 UTC
    Or, more idiomatically:
    my @everything = (\@fw, \@vlan, \@vrf, \@more); foreach (@everything) { theSub(@$_); }
    (or:  theSub($_); depending on your usage).

      How is that more idiomatic? for and foreach are synonyms.

      Perhaps the use of $_ instead of the named variable is what you're referring to, but I am not convinced that that is more idiomatic.

        That's right. I meant the use of $_
Re^2: array or array ref as variable
by Hossein (Acolyte) on Jun 28, 2013 at 07:09 UTC

    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?

      ( 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2015-07-08 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls