Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pring values from Perl Objects

Status
Not open for further replies.

rkumar28

MIS
Jan 30, 2005
15
US
I have a main function called make_file() that gets all the IDs and Associated filenames into an hash array ($href_data). This hash is getting passed into another function make_file() where an object 'hr_data_file' (in blue text below) is getting created. All I am trying to do is to call this object 'hr_data_file' into get_leaves() function and print the associated IDs and Names and other fields.

I seek for help and advice on how to print the fields form this object into the get_leaves() function. Please see the Text in red below where I am trying to print the values from the 'hr_data_file'. I tried some suggestions made in this forum on priting Hash arrays but did not seems to work, probably I am doing something wrong.
Will really appreciate any advice on this.


#!/usr/bin/perl

.....Some code..........
.........................

$href_data = make_file();

$aref_all_groups = make_groups($links,0,$max_links, $href_data);


sub make_file
{
...................................................
...........................................

while ( $hash_refrence = $std->fetchrow_hashref )
{
push @{$file_return->{$hash_refrence->{'ID'}}}, $hash_reference;
$node_index++;
}

return (file_return);
}


sub make_groups
{

($aref_links, $curr_link_counter, $max_link, $href_data ) = @_;

.......Some code comes here.......

if ( $curr_child_type_cde eq 'F')
{
$href_child=
{
'group_name' => $curr_group_name,
'group_id' => $curr_child_file_id,
'group_type' => $curr_group_type,
'hr_data_file' => $href_data->{$curr_child_file_id} ---->object is getting created.
};


# print "The hr_data_file is: ",Dumper($href_data->{$curr_child_file_id}),"\n";

$$ar_child_files[$child_file_counter++]=$href_child;

return($aref_groups);

}



sub get_leaves
{

....Some code.........
.........................

($G1, $a_files, $level) = @_;


$ar_files=$G1->{'ar_child_files'};

for $F1 ( @$ar_files )
{

print $F1->{'hr_data_file'}{'FL_NM'}; ---->How to grab the values from the Object hr_data_file.

print "SpSheet.ActiveSheet.Cells($cols[$PREFIX_DESC], $PREFIX_DESC).Value = \"$F1->{'hr_data_file'}{'FL_DSC'}\"\n";
$cols[$PREFIX_DESC]++;
}

}



 
What is the reference $aref_groups returned by sub make_groups ?

Your subroutine returns into $ar_files the reference $aref_groups, but you want print the data which are referenced by $href_child, is that so?

So the possible solution would be if you change your subroutine make_groups so, that it returns $href_child.
Code:
sub make_groups
{

   ($aref_links, $curr_link_counter, $max_link, $href_data ) = @_;

.......Some code comes here.......

     if ( $curr_child_type_cde eq 'F')
     {
        $href_child= 
        {
           'group_name'        => $curr_group_name,
           'group_id'          => $curr_child_file_id, 
           'group_type'        => $curr_group_type,
          'hr_data_file'      => $href_data->{$curr_child_file_id}   ---->object is getting created.
        }; 

     # print "The hr_data_file is: ",Dumper($href_data->{$curr_child_file_id}),"\n";
 
        $$ar_child_files[$child_file_counter++]=$href_child;

[COLOR=red]return($href_child);[/color]

}
 
Hi,
Thanks a bunch for taking time reading my problem and replying. I tried changing the return statement to return($href_child); but I hit the error at other place and my program is not even getting to sub get_leaves function. Actually make_group function has to return $aref_groups as this function is creating groups and child($href_child). The perl program was long hence could not paste the whole program. I only pasted that I felt was required. I have pasted the whole make_group function in the end. But my problem is in the sub get_leaves function.


The whole PERL program was working fine before. The were changes were made in the sub make_file function and Hash of Hash was changed to Hash Array. (Please see below in the sub make_file function). After that I started getting error. Before the changes were made in the sub make_file function, I was able to print the fields from the 'hr_data_file' in the sub get_leaves function. But after the sub make_file function was changed to Hash array it started given error in the sub get_leaves function...: Bad index while coercing array into hash at line 278 and line 278 happen to be inside the sub get_leaves function where it is trying to print the field from hr_data_file(see line in blue below).


LINE 278: $F1->{'hr_data_file'}{'FL_NM'}; --->I am getting the Bad index while coercing array into hash error in the sub get_leaves function

Is there a way to fix the error I am hitting. If I change the line below and add index to it..it prints fine
$F1->{'hr_data_file'}[0]{'FL_NM'};---->I don't hit error when I type in the index.
$F1->{'hr_data_file'}{'FL_NM'}; ---->If I remove the Index...I hit error with this. Is there a way to change this to Hash Array in the sub get_leaves function..


sub make_file
{
     ...................................................
      ...........................................
$hash_return = {};
$node_index=0;

/* This is the old code . With this code I was able to print the hr_data_file fields in the sub get_leaves function */

# while ( $hash_refrence = $std->fetchrow_hashref )
# {
# $file_return->{$hash_refrence->{'ID'}}=$hash_refrence;
# $node_index++;
# }

/* The above code was changed to the one below */
    while ( $hash_refrence = $std->fetchrow_hashref )
     {   
       push @{$file_return->{$hash_refrence->{'ID'}}}, $hash_reference;   
       $node_index++;
    }

return (file_return);
}


sub get_leaves
{

my ($ar_groups, $aref_files, $F1, $F2, $G1, $G2, $level);
my ($i, @cols, @found_files, $file_reg_expion );
($G1, $aref_files, $level) = @_;
$ar_files=$G1->{'ar_child_files'};
if ( @$ar_files > 0 )
{
for $F1 ( @$ar_files )
{
$F1->{'hr_data_file'}[0]{'FL_NM'}; ---->I don't hit error when I type in the index.
$F1->{'hr_data_file'}{'FL_NM'}; ---->If I remove the Index...I hit error with this. Is there a way to print this as Hash Array..

/* I think if I can get change this to HASH ARRAY this should print fine */
print $F1->{'hr_data_file'}{'FL_NM'};

print "SpSheet.ActiveSheet.Cells($cols[$PREFIX_DESC], $PREFIX_DESC).Value = \"$F1->{'hr_data_file'}{'FL_DSC'}\"\n";
$cols[$PREFIX_DESC]++;
}

}
}


sub make_groups
{
my ( $aref_groups, $aref_counter, $curr_link_counter, $my_current_counter, $max_link, $aref_links, $href_links, $href_return);
my ( $href_child, $href_data, $ar_child_files, $ar_child_groups, $ar_child_unknowns);
my ( $hr_sub_group, $child_file_counter, $child_group_counter, $child_unknown_counter );

($aref_links, $curr_link_counter, $max_link, $href_data ) = @_;


$aref_groups = [];
$aref_counter = 0;

$old_group_name = "";
$old_group_id=0;

$ar_child_files=[];
$ar_child_groups=[];
$ar_child_unknowns=[];

$child_file_counter=0;
$child_group_counter=0;
$child_unknown_counter=0;

while ( $curr_link_counter < $max_link )
{
$curr_group_id = @$aref_links[$curr_link_counter]->{'PRNT_CNTL_GRP_ID'};
$curr_child_file_id = @$aref_links[$curr_link_counter]->{'CHILD_RLTN_ID'};
$curr_child_type_cde=@$aref_links[$curr_link_counter]->{'CHILD_RLTN_TYP_CDE'};
$curr_group_name = @$aref_links[$curr_link_counter]->{'CNTL_GRP_NM'};
$curr_group_type = @$aref_links[$curr_link_counter]->{'CNTL_RLTN_TYP_CDE'};

### Top Level Groups ######################
if ( $curr_group_id ne $old_group_id )
{

$ar_child_files=[];
$child_file_counter=0;

$ar_child_groups=[];
$child_group_counter=0;

$href_return =
{
'group_name' => $curr_group_name,
'group_id' => $curr_group_id,
'group_desc' => @$aref_links[$curr_link_counter]->{'CNTL_GRP_DSC'},
'ar_child_files' => $ar_child_files,
'ar_child_groups' => $ar_child_groups,
'ar_child_unknowns' => $ar_child_unknowns
};

$$aref_groups[$aref_counter++]=$href_return;
$old_group_id = $curr_group_id;
} ##################################


### Files ######################################
if ( $curr_child_type_cde eq 'F')
{
$href_child=
{
'group_name' => $curr_group_name,
'group_id' => $curr_child_file_id,
'group_type' => $curr_group_type,
'hr_data_file' => $href_data->{$curr_child_file_id}
};
# print "The hr_data_file is: ",Dumper($href_data->{$curr_child_file_id}),"\n";
$$ar_child_files[$child_file_counter++]=$href_child;
}

### Groups ######################################
elsif ( $curr_child_type_cde eq 'G')
{
($sub_group_found, $hr_sub_group) = get_sub_group($aref_groups,$curr_child_file_id);

$href_child=
{
'group_name' => $curr_group_name,
'group_type' => $curr_group_type,
'group_id' => $curr_child_file_id,
'sub_group_found' => $sub_group_found,
'hr_data_group' => ($sub_group_found ? $hr_sub_group : null)

};
$$ar_child_groups[$child_group_counter++]=$href_child;
}

### Unknowns ######################################
else
{
$href_child=
{
'group_name' => $curr_group_name,
'group_id' => $curr_child_file_id
};
$$ar_child_unknowns[$child_unknown_counter++]=$href_child;
}

$curr_link_counter++;
}

return($aref_groups);
}

 
Hi,
I think I found the problem. I found little similar post in this forum. I tried implementing it and it seems to work.
I changed the code in the sub get_leaves function as below and it seems to work fine:

sub show_leaves
{

my ($ar_groups, $aref_files, $F1, $F2, $G1, $G2, $level);
my ($i, @cols, @found_files, $file_reg_expion );

($G1, $aref_files, $level) = @_;

$ar_files=$G1->{'ar_child_files'};

if ( @$ar_files > 0 )
{
for $F1 ( @$ar_files )
{
foreach my $record (@{$F1->{'hr_data_file'}}) {

print $record->{'FL_NM'};


.....More Code comes here

}
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top