i have this sources first start.pl
use strict;
use warnings;
use hh;
print "Starting...\n";
my $abc={
VAL=>['alfa','beta'],
STUDENTS=>{
stud1=>{VAL=>['delta','gama']},
stud2=>{VAL=>['omega','upsilon']},
}
};
my $object=hh->new(catalog=>$abc);
and the package
package hh;
use strict;
use warnings;
sub new {
my $class=shift;
my $self={@_};
bless ($self,$class) ;
$self->_initialize("",['BEGIN']);
return $self ;
}
sub _initialize {
my $self=shift;
my $tab=shift;
my $carry=shift;
$tab=$tab."\t|";
if (defined $self->{VAL}){print "$tab Have val=",join(" ",@{$self->{VAL}}),"\n";push(@{$carry},@{$self->{VAL}})}
foreach my $k (sort {lc $a cmp lc $b} keys %{$self}){
print $tab,"carry=",$#{$carry}+1," "," ",$k,"\n";
if (ref($self->{$k}) eq 'HASH'){print "$tab Running initialize pentru $k\n";_initialize($self->{$k},$tab,$carry)}
}
return $self;
}
1;
the output is like this
Starting...
|carry=1 catalog
| Running initialize pentru catalog
| | Have val=alfa beta
| |carry=3 STUDENTS
| | Running initialize pentru STUDENTS
| | |carry=3 stud1
| | | Running initialize pentru stud1
| | | | Have val=delta gama
| | | |carry=5 VAL
| | |carry=5 stud2
| | | Running initialize pentru stud2
| | | | Have val=omega upsilon
| | | |carry=7 VAL
| |carry=7 VAL
Somehow i want to collect into an array the VAL arrays between top of tree and the walked element . Way the last VAL who is in the second level has 7 elements . I want to have only 2 elements (alfa and beta)
this is the expected output
Starting... |carry=1 catalog | Running initialize pentru catalog | | Have val=alfa beta | |carry=3 STUDENTS | | Running initialize pentru STUDENTS | | |carry=3 stud1 | | | Running initialize pentru stud1 | | | | Have val=delta gama | | | |carry=5 VAL carry=5 (OK) | | |carry=5 stud2 | | | Running initialize pentru stud2 | | | | Have val=omega upsilon | | | |carry=7 VAL carry=5 (not ok this need only his values catalog alfa beta omega epsilon) | |carry=7 VAL not ok (carry 3 only catalog alfa beta)
...