Hi -
This looks like a hash to me:
my %color_hash = {
"sky" => "blue",
"grass" => "green",
"apple" => "red",
};
for my $key (keys %color_hash) {
print "$key => $color_hash{$key}\n";
}
Aloha - Beau.
-----Original Message-----
From: Etienne Marcotte [mailto:[EMAIL PROTECTED]]
Sent: Friday, October 26, 2001 4:52 AM
To: Brian Arnold
Cc: 'Neclos'; [EMAIL PROTECTED]
Subject: Re: create hash and print all elements?
I have a question related to your array of array..
Let's say I have
my @color_array = (
["sky", "blue"],
["grass", "green"],
["apple", "red"]
);
I'd like to print: sky, grass, apple = blue, green, red
Right now I'm using two arrays, one for the "items" and one for the
"colors"...
and do my $list_items = join (", ", @items); $list_colors = join (", ",
@colors);
print "$list_tiems = "list_colors";
(assuming @items = qw(sky grass apple); @colors = qw(blue green red);
How can it be done with only one @color? And if it's more compelx, do you
really thing it's useful to do it like this or my way is ok?
Etienne
Brian Arnold wrote:
> > I need to create a hash with fixed elements and print all elements.
>
> I think that people keep missing this when they send their answers.
> They're telling you how to print the hash, which is helpful, but not how
> to keep the sequence of items. Because of the way that hashing is done,
> no hash is fixed. If it were fixed, then it'd lose most of it's
> efficiency.
>
> When you call the keys function on a hash, it just returns a list of the
> keys in no specific (or reliable) order. Calling the sort function just
> sorts them alphabetically.
>
> In order to preserve sequence, you could do something
> not-so-easy-to-read, like this.
>
> use strict;
> my @color_array = (
> ["sky", "blue"],
> ["grass", "green"],
> ["apple", "red"]
> );
>
> foreach (0..$#color_array) {
> print "$color_array[$_]->[0]: $color_array[$_]->[1]\n";
> }
>
> print "Pushing a yellow sun into the array...\n";
> push @color_array, ["sun", "yellow"];
>
> foreach (0..$#color_array) {
> print "$color_array[$_]->[0]: $color_array[$_]->[1]\n";
> }
>
> This code should work (I actually tested it - imagine that) - it isn't
> exactly intuitive or easy to use, but it preserves the sequence, and you
> can push items onto it later.
>
> ====================
> Brian Arnold
> [EMAIL PROTECTED]
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]