I want to use the array because I have multiple ftp servers that can be called 
from a dropdown box. Is it possible to do this with just a hash?

Thanks

-----Original Message-----
From: Steve Bertrand [mailto:st...@ibctech.ca]
Sent: Friday, March 26, 2010 8:55 AM
To: Pry, Jeffrey
Cc: beginners@perl.org
Subject: Re: Storing a Hash Inside An Array

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

IMPORTANT: The information contained in this email and/or its attachments is 
confidential. If you are not the intended recipient, please notify the sender 
immediately by reply and immediately delete this message and all its 
attachments. Any review, use, reproduction, disclosure or dissemination of this 
message or any attachment by an unintended recipient is strictly prohibited. 
Neither this message nor any attachment is intended as or should be construed 
as an offer, solicitation or recommendation to buy or sell any security or 
other financial instrument. Neither the sender, his or her employer nor any of 
their respective affiliates makes any warranties as to the completeness or 
accuracy of any of the information contained herein or that this message or any 
of its attachments is free of viruses.

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