On 2010.03.26 08:36, Pry, Jeffrey wrote:
> Hey,
> 
> First off let me say that this mailing list is great! It is nice to be able 
> to see the collaborative effort that everyone puts in when one of us has a 
> question or issue. Thank you!
> 
> Now, I was wondering if it would be possible to store a hash inside of an 
> array. I would like to have an array called @settings and a hash inside of 
> that containing the settings for each array item. Ideally the @array would 
> contain a hash at @array[i] where settings such as 'ftpserver' = localhost, 
> 'password' = p...@$$word, etc. I want to do this so I can separate records in 
> the array and call them only if needed and also to make it easier to add 
> additional entries in the future to my application.
> 
> Thanks in advance for all of your help! It is much appreciated,

I can't understand why you want to use the array at all. Unless I'm
missing something, just use a hash directly:

#!/usr/bin/perl

use strict;
use warnings;

my %config;

$config{ ftpserver } = 'localhost';
$config{ ftpuser }   = 'steve';
$config{ ftppass }   = 'blah';

# print out all

while ( my ( $directive, $value ) = ( each %config ))  {
    print "$directive: $value\n";
}

# print out specific

print "Host: $config{ ftpserver }\n";

# add new

$config{ use_ssh } = 'yes';

# print all again

while ( my ( $directive, $value ) = ( each %config ))  {
    print "$directive: $value\n";
}

If you really do want to put a hash into an array, use a hash reference:

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my @settings;

push @settings, { ftpserver => 'host', ftpuser => 'steve' };

# could also be done the non-anonymous way:

# my $hash = { ftpserver => 'host', ftpuser => 'steve' };
# push @settings, $hash;

print Dumper @settings;

# prints:

$VAR1 = {
          'ftpuser' => 'steve',
          'ftpserver' => 'host'
        };

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to