Thread Übergabe Hash-Struktur an Methode (33 answers)
Opened by mtbf40 at 2015-05-19 15:44

mtbf40
 2015-05-27 07:11
#181185 #181185
User since
2010-06-03
17 Artikel
BenutzerIn
[default_avatar]
... und weiter gehts

der Hash erweitert sich jetzt noch um ein Array

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl

use warnings;
use strict;
use DateTime qw();
use feature qw/say switch/;

package childmodul;

  sub new {    
    my ($classname,$hsh_scriptParam) = @_;
    my $self = {%{$hsh_scriptParam}};
    return bless($self, $classname);
  }

  sub set_Data {
    my $self = shift;
    my $val  = shift;
    my @locations = @_;

    # save last key
    my $last_key = pop @locations;

    # helper ref for each level inside HoHoH...
    my $ref = $self;
    for my $location ( @locations ) {
      # create new hash reference at this level if not already there
      $ref->{$location} = {}  if !exists $ref->{$location};

      # this points to the next level down the hash tree
      $ref = $ref->{$location};
    }

    # save value in last key
    if (ref($last_key) eq 'ARRAY') {
      $ref->[$last_key->[0]] = $val;
    } else {
      if (ref($val) eq 'HASH') {
        $ref->{$last_key} = $val->{hash};
      } else {
        $ref->{$last_key} = $val;
      }
    }
  }


package main;

my $hsh_runtimeParam = {
  ScriptParam => {ScriptName => $0 || '???',
                  Version    => 'v.01' || '???',
                  Hilfe      => {Usage   => 'ruf mich mal richtig auf',
                                 Message => 'rufe mal nach Mutti'
                                },
                 },
  Runtime     => {ApplName   => 'TestClass',
                  Caller     => $^O =~ /win32/i ? $ENV{USERNAME} : $ENV{USER},
                  Loglevel   => 'höher geht nicht',
                  RootPath   => 'ganz oben' || '???',
                  StartTime  => 'am Anfang' || '???',
                  Instanz    => [ 'test1', 'test2', '11', 'array' ]
                 },
  DBParam     => {RegPar     => 'test',
                  dbSession  => {ApplName => 'testhash',
                  Action     => 'insert',
                  runtime    => {dbTable  => 'INIT_REG'}}
                 },
};

my $Object1 = childmodul->new($hsh_runtimeParam);

my @h_keys = qw(Runtime Instanz);
push @h_keys, [3];

$Object1->set_Data('hash', @h_keys );

$Object1->set_Data({hash=>{
                    RUN_ENDDATE   => ["to_date(?,'DD.MM.YYYY HH24:MI:SS')",DateTime->now(time_zone => 'local')->strftime('%d.%m.%Y %H:%M:%S')],
                    RUN_LOGMSG    => 'Message',
                    RUN_RC_CODE   => '12'
                   }},
                   qw(DBParam RunPar)
                 );

$Object1->set_Data('update', qw( DBParam dbSession Action ));

$Object1->set_Data('debug', qw( Runtime Loglevel ));

print 'Ende' . "\n";


so jetzt soll z.B. Runtime->Instanz->[3] geändert werden in "hash"
das ist jetzt meine Lösung
Last edited: 2015-05-27 11:16:16 +0200 (CEST)

View full thread Übergabe Hash-Struktur an Methode