Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

wie greife ich auf ein Array von Hashes zu?

15 views
Skip to first unread message

Astrid

unread,
Jul 25, 2017, 3:44:18 AM7/25/17
to
Hallo!

Habe ein Hash, der Arrays enthaelt.
Den Hash habe ich wieder in ein Array gepackt.

Wie greife ich aber nun auf so ein Element zu?

Ein Testbeispiel (das eigentliche Programm ist
komplexer).

#!/usr/bin/perl -w

my %hierhash;
my @hashfeld;

@hashfeld = ();

%hierhash = &dofn(0,5,1);
print "$hierhash{'var2'}[3] \n";
push(@hashfeld,%hierhash);

%hierhash = &dofn(0,5,2);
print "$hierhash{'var2'}[3] \n";
push(@hashfeld,%hierhash);

print $hashfeld[1]{'var2'}[3];
print "\n";

sub dofn () {
my ($i, $ivon, $ibis, $imal);
my %hashfn;

$ivon = $_[0];
$ibis = $_[1];
$imal = $_[2];

for ($i = $ivon; $i < $ibis; $i++) {
push(@{$hashfn{'var1'}}, $i*$imal);
push(@{$hashfn{'var2'}}, $i*$imal*10.);
push(@{$hashfn{'var3'}}, $i*$imal*20.);
}

return %hashfn;
}

Gruss, Astrid

Megalodon

unread,
Jul 25, 2017, 10:01:26 AM7/25/17
to
Hi!

Dein Array @hashfeld hat nicht die beiden Hashes drinnen, sondern den Inhalt der Beiden. Das greift darauf zu:
print $hashfeld[1][6][3] + '\n';

Willst du hashes haben dann füge {} hinzu:
push(@hashfeld,{%hierhash});

Dann funktioniert auch
print $hashfeld[1]{'var2'}[3];

Grüße,
Wolf

Astrid

unread,
Jul 25, 2017, 11:01:12 AM7/25/17
to
Danke. :)

0 new messages