Home > Archive > PERL Beginners > July 2005 > Hash reference structure
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]
| Author |
Hash reference structure
|
|
| Jan Eden 2005-07-24, 8:29 pm |
| Hi,
I need to store a list of month names into a hash structure such that:
$self->{monate}->{1}->{bezeichnung} =3D 'Januar'
$self->{monate}->{2}->{bezeichnung} =3D 'Februar'
etc.
Until now, I have used the rather clumsy:
@month_hash{1..12} =3D ("Januar", "Februar", "M=E4rz", "April", "Mai", "Jun=
i", "Juli", "August", "September", "Oktober", "November", "Dezember");
for (sort keys %month_hash) { $self->{monate}->{$_}->{bezeichnung} =3D $mon=
th_hash{$_}; }
The following did not work:
@{$self->{monate}->{1 ..12}->{bezeichnung}} =3D ("Januar", "Februar", "M=E4=
rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "Nov=
ember", "Dezember");
Is there a better way to do it? These references are making my brain swirl.
Thanks,
Jan
--=20
A good programmer is someone who looks both ways before crossing a one-way =
street. - Doug Linder
| |
| Xavier Noria 2005-07-24, 8:29 pm |
| On Jul 21, 2005, at 17:21, Jan Eden wrote:
> Hi,
>
> I need to store a list of month names into a hash structure such that:
>
> $self->{monate}->{1}->{bezeichnung} =3D 'Januar'
> $self->{monate}->{2}->{bezeichnung} =3D 'Februar'
> etc.
>
> Until now, I have used the rather clumsy:
>
> @month_hash{1..12} =3D ("Januar", "Februar", "M=E4rz", "April", "Mai", =
=20
> "Juni", "Juli", "August", "September", "Oktober", "November", =20
> "Dezember");
>
> for (sort keys %month_hash) { $self->{monate}->{$_}->{bezeichnung} =20
> =3D $month_hash{$_}; }
Basically the same, only using an array for the months (which is more =20=
natural for its use in that snippet, and avoids the "sort keys"), and =20=
not using arrows in nested estructures, since they are optional and =20
the code is more readable without them:
my @months =3D qw(Januar Februar M=E4rz April Mai Juni Juli August =20=
September Oktober November Dezember);
$self->{monate}{$_}{bezeichnung} =3D $m[$_-1] for 1..@months;
-- fxn=
| |
| Bob Showalter 2005-07-24, 8:29 pm |
| Jan Eden wrote:
> Hi,
>=20
> I need to store a list of month names into a hash structure such =
that:
>=20
> $self->{monate}->{1}->{bezeichnung} =3D 'Januar'
> $self->{monate}->{2}->{bezeichnung} =3D 'Februar'
> etc.
>=20
> Until now, I have used the rather clumsy:
>=20
> @month_hash{1..12} =3D ("Januar", "Februar", "M=E4rz", "April", =
"Mai",
> "Juni", "Juli", "August", "September", "Oktober", "November",
> "Dezember"); =20
>=20
> for (sort keys %month_hash) { $self->{monate}->{$_}->{bezeichnung} =
=3D
> $month_hash{$_}; }=20
You don't need to sort the keys, you don't need the arrows between =
adjacent
braces, and you can use the 'for' modifier to make the statement a bit =
more
readable:
$self->{monate}{$_}{bezeichnung} =3D $month_hash{$_} for 1..12;
>=20
> The following did not work:
>=20
> @{$self->{monate}->{1 ..12}->{bezeichnung}} =3D ("Januar", "Februar",
> "M=E4rz", "April", "Mai", "Juni", "Juli", "August", "September",
> "Oktober", "November", "Dezember"); =20
>=20
> Is there a better way to do it? These references are making my brain
> swirl.=20
If you want to avoid using %month_hash and want to use a list =
initializer,
you could do something like:
my $n =3D 0;
$self->{monate}{++$n}{bezeichnung} =3D $_ for qw/
Januar Februar M=E4rz April Mai Juni=20
Juli August September Oktober November Dezember
/;
| |
| Bob Showalter 2005-07-24, 8:29 pm |
| Bob Showalter wrote:
>
> $self->{monate}{$_}{bezeichnung} = $month_hash{$_} for 1..12;
>
That could also be 'for keys %month_hash'
| |
| Jan Eden 2005-07-24, 8:29 pm |
| Thanks, Xavier and Bob,
that looks much better.
Cheers,
Jan
Bob Showalter wrote on 21.07.2005:
>Jan Eden wrote:
>
>You don't need to sort the keys, you don't need the arrows between adjacen=
t
>braces, and you can use the 'for' modifier to make the statement a bit mor=
e
>readable:
>
> $self->{monate}{$_}{bezeichnung} =3D $month_hash{$_} for 1..12;
>
>
>If you want to avoid using %month_hash and want to use a list initializer,
>you could do something like:
>
> my $n =3D 0;
> $self->{monate}{++$n}{bezeichnung} =3D $_ for qw/
> Januar Februar M=E4rz April Mai Juni=20
> Juli August September Oktober November Dezember
> /;
>
--=20
A core dump is your computer's way of saying "Here's what's on my mind, wha=
t's on yours?"
| |
| Scott R. Godin 2005-07-24, 8:29 pm |
| Jan Eden wrote:
> Hi,
>
> I need to store a list of month names into a hash structure such that:
>
> $self->{monate}->{1}->{bezeichnung} = 'Januar'
> $self->{monate}->{2}->{bezeichnung} = 'Februar'
> etc.
>
> Until now, I have used the rather clumsy:
>
> @month_hash{1..12} = ("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
definitely would make more sense to use an array here, since you're
indexing via numeric value.
@months = qw(Januar Februar März April Mai Juni Juli August September
Oktober November Dezember);
> for (sort keys %month_hash) { $self->{monate}->{$_}->{bezeichnung} = $month_hash{$_}; }
and
$self->{monate}{$_}{bezeichnung} = $months[$_ - 1] for 1..12;
|
|
|
|
|