Schrift
[thread]7609[/thread]

Hashes zusammenlegen



<< >> 5 Einträge, 1 Seite
bloonix
 2006-01-11 18:47
#61674 #61674
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Hallo,

folgendes ...

ich habe drei Statement, die jeweils nur eine Zeile wieder-
geben. Nun möchte ich die drei Hashes gerne in einem Hash
(Referenz) liegen haben.

Bei diesem Code hier

Code: (dl )
1
2
3
my $daten = $sth->fetchrow_hashref;
my $kat = $sth->fetchrow_hashref;
my $ber = $sth->fetchrow_hashref;


müsste ich demnach die Keys aus %$kat und %$ber nach
%$daten schieben.

Code: (dl )
1
2
3
4
5
for (my ($key,$val) = each %{$ber}) {
  $daten->{$key} = $val;
}

# das gleiche für $kat


Gibt es nicht elegantere Methoden? Sowas wie ein Hash-Push?\n\n

<!--EDIT|opi|1136999003-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
Ronnie
 2006-01-11 18:56
#61675 #61675
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Zuweisung in einem Listen-Kontext wäre so eine Möglichkeit:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %color1 = ( blue => '#00f', red => '#f00');
my %color2 = ( pink => '#f0f', yel => '#ff0');

%color1 = (%color1, %color2);

print Dumper \%color1;;
\n\n

<!--EDIT|Ronnie|1136998634-->
bloonix
 2006-01-11 19:19
#61676 #61676
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
[quote=Ronnie,11.01.2006, 17:56]Zuweisung in einem Listen-Kontext wäre so eine Möglichkeit:
Code: (dl )
%color1 = (%color1, %color2);
[/quote]
Hallo Ronnie,

danke für deine schnelle Antwort.

Hast du auch ne Idee wie man das mit Referenzen machen
könnte?

Greez,
opi
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
Taulmarill
 2006-01-11 19:23
#61677 #61677
User since
2004-02-19
1750 Artikel
BenutzerIn

user image
im prinzip genau so, du musst die referenzen nur dereferenzieren um sie wie hashes benutzen zu können. das hast du ja oben auch schon selbst vorgeschlagen.
$_=unpack"B*",~pack"H*",$_ and y&1|0& |#&&print"$_\n"for@.=qw BFA2F7C39139F45F78
0A28104594444504400 0A2F107D54447DE7800 0A2110453444450500 73CF1045138445F4800 0
F3EF2044E3D17DE 8A08A0451412411 F3CF207DF41C79E 820A20451412414 83E93C4513D17D2B
bloonix
 2006-01-11 19:31
#61678 #61678
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
[quote=Taulmarill,11.01.2006, 18:23]im prinzip genau so, du musst die referenzen nur dereferenzieren um sie wie hashes benutzen zu können. das hast du ja oben auch schon selbst vorgeschlagen.[/quote]
*ARG*

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
#!/usr/local/bin/perl -w
use strict;

use Data::Dumper;

my $color1 = { blue => '#00f', red => '#f00'};
my $color2 = { pink => '#f0f', yel => '#ff0'};

my %color1 = (%{$color1}, %{$color2});

print Dumper \%color1;


klaro! wie dumm von mir :)

danke!
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
<< >> 5 Einträge, 1 Seite



View all threads created 2006-01-11 18:47.