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

array or array ref as variable

by Hossein (Acolyte)
on Jun 28, 2013 at 05:55 UTC ( #1041148=perlquestion: print w/replies, xml ) Need Help??
Hossein has asked for the wisdom of the Perl Monks concerning the following question:


I have some @arrays.

I would like to have an array containing (ref to) these arrays.

f.eks. my arrays are: @fw, @vlan, @vrf, @more i need some thing like this: @everything[\@fw, \@vlan, \@vrf, \@more] foreach (@everything) { send $_ to sub_as_array }

Replies are listed 'Best First'.
Re: array or array ref as variable
by GrandFather (Sage) on Jun 28, 2013 at 06:20 UTC

    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
      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.

      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)


Re: array or array ref as variable
by GrandFather (Sage) on Jun 28, 2013 at 11:57 UTC

    Having looked over your conversation with HelenCr, I think a slight step back is in order to focus on the key issue. The following code mocks up a solution to the issue of passing in a "module reference" to a "collection" sub to get some work done:

    #!/usr/bin/env perl use warnings; use strict; package FW; sub extract { return "It's a FW"; } package VRF; sub extract { return "It's a VRF"; } package main; my @fw = collect('fw'); my @vrf = collect('vrf'); print "$_\n" for @fw, @vrf; sub collect { my ($type) = @_; my $module = uc $type; return $module->extract(); }


    It's a FW It's a VRF

    The key is that if you want to call a function in a module you can use the syntax $modulename->function() to make the call.

    True laziness is hard work
      Thank you, GrandFather.

      So, in other words, are you saying that this should work?:

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041148]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-07-20 20:12 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (314 votes). Check out past polls.