Balls McWang has asked for the wisdom of the Perl Monks concerning the following question:

Heynow!
I'm not sure if what I'm doing here is a reference (E-Eng trying to learn Perl on my own) so sorry if this is a waste of your time. Normally I'm able to solve my problems by using the search engine.

I'm trying to create an array name from a line of information I've found in another array. Then I would like to be able to push data into that array and print it out to a file later on.

What I think I need to do is use a reference ... but I don't think I'm doing it correctly and I'm getting lost Programming Perl: Chapter 8. I use references in Tk and other modules but I've always just learned by example

Realise I'm way off here and would appreciate help even if it's just to a page in the book or a node I should read.

Here's what I'm doing ... part of a bigger file.

Thanks,
James

scalar(@dev_list) == $#dev_list + 1; scalar(@xref) == $#xref + 1; for ($i=0; $i < (scalar(@dev_list)); $i++) { print STDOUT "Looking for $dev_list[$i]\n"; for ($c=0;$c < (scalar(@xref)); $c++) { if($xref[$c] =~ /$dev_list[$i]\./i) { $templine = join "_","\@dev_xref",$dev_list[$i]; $dev_array_ref = \($templine); push(\$dev_array_ref, $xref[$c]); } } }

Edit: Corrected the <code> tag. larsen

Replies are listed 'Best First'.
Re: creating an array name using information from another array
by waswas-fng (Curate) on Mar 04, 2004 at 19:36 UTC
    Wanting variable variable names tend to point tword a misundertanding of the language.. Here (part one,part two,part three) are some reasons why you should not use them...


    -Waswas
Re: creating an array name using information from another array
by maa (Pilgrim) on Mar 04, 2004 at 19:19 UTC

    Hi,

    I'm not sure what the point of the lines scalar(@dev_list) == $#dev_list + 1; scalar(@xref) == $#xref + 1; is since == is an equality test - are these lines supposed to be comments for you?

    A more perlish way to write your nested for loops would be:

    for my $devitem (@dev_list) { print "Looking for $devitem\n"; for my $xrefitem (@xref) { if ($xrefitem =~ /$devitem/i) { #Do you really need to match a +nother character? #What's the next line for? # $templine=join "_","\@dev_xref",$dev_list[$i]; my @{$templine}[0]=$xrefitem; } }

    I'm not entirely sure that I understood what you were trying to do but you can create variables named using the contrents of another variable using the ${$varname} style syntax. But then you have to store your created variable's name so you can access it later... it might be easier to pre-dclare a hash and use the keys function.

    HTH - Mark

Re: creating an array name using information from another array
by NetWallah (Canon) on Mar 04, 2004 at 19:22 UTC
    In addition to what maa has said, I think what you are looking for is a HASH of ARRAYS (Which will, in reality, be a Hash or of array-references)- this can store arrays, and access them by a user-defined name. Please read perldoc perlref. Sample:
    my %HoA = { FirstVar=> [qw(Firstref Secondref etc)], SecondVar => [qw(SecondvarRef1 Ref2 etcetc)]}; #To access, my $varname = 'FirstVar'; foreach (@{$HoA{$varname}}{ print "Variable $varname ref: $_\n"; }
    Update: Fixed Typo
Re: creating an array name using information from another array
by graff (Chancellor) on Mar 05, 2004 at 04:28 UTC
    To put the earlier replies together, something like this might be close to what you want (not tested):
    my %xref_hash; for my $dev ( @dev_list ) { print STDOUT "looking for $dev\n"; for my $x ( @xref ) { if ( $x =~ /$dev\./i ) { my $hkey = "dev_xref_$dev"; push @{ $xref_hash{ $hkey }}, $x; } } } # to use that data structure: for my $hkey ( keys %xref_hash ) # (you might want to sort) { print join( "\n ", "\nvalues for $hkey:", @{$xref_hash{$hkey}} ), +"\n"; }
      Thanks to all for the help

      Realised last night that I was trying to make it too complex and ended up doing something a lot easier. I got my head caught up around the concept that I needed what I thought were "annoymous references" when there was a much easier way to do it.

      I'm looking forward to reading the nodes you have all posted for me to look at ... and to learning more Perl