Home > Archive > PERL Miscellaneous > March 2004 > Hashes of arrays
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I want to make an associative array containing arrays rather
than scalar values, and I've been trying to do it like this:
@xxx("a") = (4, 3, 6);
@xxx("b") = (7, 6, 8);
@xxx("c") = (1, 4, 3);
@xxx("d") = (0, 5, 2);
However, when I try to get the array back out of the hash, perl
just gives me the first value of the array, e.g. if I do:
while (($key, @values) = each %xxx) {
print ("$key: @values\n");
}
I'd get:
a: 4
b: 7
c: 1
d: 0
How do I get the full array back out?
| |
| Mothra 2004-03-29, 5:37 pm |
| Me wrote:
> I want to make an associative array containing arrays rather
> than scalar values, and I've been trying to do it like this:
>
> @xxx("a") = (4, 3, 6);
> @xxx("b") = (7, 6, 8);
> @xxx("c") = (1, 4, 3);
> @xxx("d") = (0, 5, 2);
>
> However, when I try to get the array back out of the hash, perl
> just gives me the first value of the array, e.g. if I do:
>
> while (($key, @values) = each %xxx) {
> print ("$key: @values\n");
> }
>
> I'd get:
> a: 4
> b: 7
> c: 1
> d: 0
>
> How do I get the full array back out?
use strict;
use warnings;
my %xxx =();
$xxx{a} = [4,3,6];
$xxx{b} = [7,6,8];
while (my ($key, $value) = each %xxx) {
print $key;
print map {$_} @$value;
}
hope this helps
Mothra
| |
| Gunnar Hjalmarsson 2004-03-29, 5:37 pm |
| Me wrote:
> I want to make an associative array containing arrays rather
> than scalar values, and I've been trying to do it like this:
>
> @xxx("a") = (4, 3, 6);
> @xxx("b") = (7, 6, 8);
> @xxx("c") = (1, 4, 3);
> @xxx("d") = (0, 5, 2);
Then I suggest that you start learning the basics about data
structures in Perl, for instance by studying
http://www.perldoc.com/perl5.8.0/pod/perldsc.html
After that you may be ready to give it a *serious* try, and if you
would encounter problems when doing so, you are welcome back here.
Also, please work with strictures and warnings enabled.
use strict;
use warnings;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
|
| In article <4068979b$1@usenet.ugs.com>, Mothra wrote:
> use strict;
> use warnings;
>
> my %xxx =();
>
> $xxx{a} = [4,3,6];
>
> $xxx{b} = [7,6,8];
>
> while (my ($key, $value) = each %xxx) {
> print $key;
> print map {$_} @$value;
> }
>
> hope this helps
That works, thanks!!!!!
| |
| Gunnar Hjalmarsson 2004-03-29, 5:37 pm |
| Me wrote:
> In article <4068979b$1@usenet.ugs.com>, Mothra wrote:
<working code>
>
> That works, thanks!!!!!
Fine. What have you learned? Or doesn't that matter?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
|
|
|