Joseph L. Casale <[EMAIL PROTECTED]> wrote:

> I  have a list:
> @list = ('Exchange','Filter','DNS','Domain');
> This is a list of arrays I also have of course to leverage 
> this I am trying to . the @ symbol on it during use.
> 
> foreach $vm (@list) {
>                 my_sub("@" . "$vm");
>                 print "@" . "$vm\n";
> }
> 
> The print likes this, but the my_sub doesn't? Why not?

If you're trying to use symbolic references, well, please
don't. They are evil.

Instead, structure your data into a hash of lists, like

#!/usr/bin/perl

use strict;
use warnings;

# initialize a hash with "array names" as keys and
# anonymous hash references as their value.

my %host = (
        'Exchange' => [ 'foo', 'baz', 'bar' ],
        'Filter' => [ 'foo2', 'baz2', 'bar2' ],
        'DNS' => [ 'foo3', 'baz3', 'bar3' ],
        'Domain' => [ 'blerg' ],
);

# the argument passed to this sub is not an array, but
# an array reference.

sub my_sub {
  my $array_ref = shift;

  # array in scalar context == number of elements
  if( @$array_ref > 1 ){
    # note -> syntax used to dereference the reference
    print "the second element is " . $array_ref->[1] . "\n";
  } else {
    print "the passed array has less than 2 elements\n";
  }
}

# keys %host is the most flexible way to specify a list of all
# key values. The order of the values is indeterminate, so you
# might have to sort to get them in the same order every time.
foreach my $vm ( keys %host ){
  print "values for $vm: " . join(', ', @{$host{$vm}} ) . "\n";
  my_sub( $host{$vm} );
}

__END__

HTH,
Thomas

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to