Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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:

Hi,

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 }

Comment on array or array ref as variable
Download Code
Re: array or array ref as variable
by GrandFather (Cardinal) 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)

        Helen

Re: array or array ref as variable
by GrandFather (Cardinal) 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(); }

    Prints:

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-12-26 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls