Thread Hash als Option missbrauchen (118 answers)
Opened by bloonix at 2005-12-19 02:24

bloonix
 2005-12-26 00:07
#61067 #61067
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
N'abend zusammen,

Hashes sind ja was ganz tolles, aber wenn man auf mehrdimensionale
Hashes zugreifen muss, dann kann die Syntax recht unübersichtlich
werden. Gibt es an dieser Stelle übliche Methoden, um die Übersichtlichkeit
zu erhöhen?

Ich habe zum Beispiel aus diesem Codestück ...

Code: (dl )
1
2
3
4
5
6
7
8
9
if ($stats->{i_stats}->{Processes}->{$pid}->{sProcStartTime} && $stats->{r_stats}->{Processes}->{$pid}->{sProcStartTime} eq $stats->{i_stats}->{Processes}->{$pid}->{sProcStartTime}) {
  for my $key (qw(sProcMinFLT sProcCMinFLT sProcMayFLT sProcCMayFLT sProcUTime sProcSTime sProcCUTime sProcCSTime)) {
     my $tmp = $stats->{r_stats}->{Processes}->{$pid}->{$key};
     $stats->{r_stats}->{Processes}->{$pid}->{$key} -= $stats->{r_stats}->{Processes}->{$pid}->{$key};
     $stats->{r_stats}->{Processes}->{$pid}->{$key} = sprintf('%.2f', $stats->{r_stats}->{Processes}->{$pid}->{$key} / ($uptime - $stats->{i_stats}->{Processes}->{uptime}))
        if $stats->{r_stats}->{Processes}->{$pid}->{$key} > 0;
        $stats->{i_stats}->{Processes}->{$pid}->{$key} = $tmp;
  }
}


das gemacht ...

Code: (dl )
1
2
3
4
5
6
7
8
9
10
my ($r_stats,$i_stats) = ($stats->{r_stats}->{Processes},$stats->{i_stats}->{Processes});

if ($i_stats->{$pid}->{sProcStartTime} && $r_stats->{$pid}->{sProcStartTime} eq $i_stats->{$pid}->{sProcStartTime}) {
  for my $key (qw(sProcMinFLT sProcCMinFLT sProcMayFLT sProcCMayFLT sProcUTime sProcSTime sProcCUTime sProcCSTime)) {
     my $tmp                   = $r_stats->{$pid}->{$key};
     $r_stats->{$pid}->{$key} -= $i_stats->{$pid}->{$key};
     $r_stats->{$pid}->{$key}  = sprintf('%.2f', $r_stats->{$pid}->{$key} / ($uptime - $i_stats->{uptime})) if $r_stats->{$pid}->{$key} > 0;
     $i_stats->{$pid}->{$key}  = $tmp;
  }
}


also ganz einfach die Referenzen auf $r_stats und $i_stats übertragen,
um $stats->{r_stats}->{Processes} nicht schreiben zu müssen.

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.

View full thread Hash als Option missbrauchen