in reply to WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?

If it doesn't work when you uncomment line 5 then perhaps $incomingdata doesn't have the value you think it does. You could use Data::Dumper to dump the value of $incomingdata. You will then have to change the calling routine to pass data that is compatible with your subroutine or change your subroutine to handle the data that is passed.

  • Comment on Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?

Replies are listed 'Best First'.
Re^2: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by Anonymous Monk on Aug 06, 2009 at 00:15 UTC
    hi ig , thaks for the attention. i really have tested what is inside the $incomedata.
    sub { #8test5 my $incomddata= $_[0]; return($incomddata); } i then call this sub and print the $incomdata in main script. i was ge +t what a wanted. as i said before $incomddata is from a loop so i was give a list of value when i print it out. it shows the data h +as been sucessfully passed into $incomddata <code> here is another sub while it runs well. however i don't understand what is wrong with first sub. i really try to fix the problem of first sub as it is easy for me to format output data layout. <code> sub { # this sub is a workable # splits them up into stuff like "{name => 'name222',colour => 'yellow +'}" my @split = (split /\Q},{/, $_[0]); my @loop; foreach (@split) { s/^{//; s/}$//; my $hash; my $hash1; my @tmp = split /,/, $_; # now lets split them at ', my @subloop; foreach my $tmp (@tmp) { my $hash; my ($name,$value) = split / => /, $tmp; $hash->{name} = $name; $hash->{value} = $value; $hash->{value} =~ s/^\'//; $hash->{value} =~ s/\'$//; push @subloop, $hash; } #$hash1->{name} = "subtotal"; #$hash1->{value} = "999"; #push(@subloop, $hash1); push @loop, { subloop => \@subloop }; } return { split_loop => \@loop }; }

      If you are certain the input to your subroutine is correct but your subroutine is returning an incorrect result then your subroutine is not written correctly. I can't help you fix the problem because you haven't shown me what input the subroutine receives or what result it must return. My uncertainty is only increased by the difference between the code in your original post and that in your recent post.

      You might find brian's Guide to Solving Any Perl Problem helpful. It has some good advice about how to find and correct the causes of errors.

      I can only help you further if you post what your input is and what your output must be. The best way to show this would be to present perl code which produces the required data structures, similar to what Data::Dumper produces. For the subroutine input, it would be best if your put the following at the beginning of your subroutine, before anything else in your subroutine, and posted the output.

      { use Data::Dumper; my $log = "/tmp/log.txt"; # change to some convenient path open(my $fh, '>', $log) or die "$log: $!"; print $fh, Dumper(\@_); close($fh); }

      If you post what gets written to the log file, then I will at least know what the input to your subroutine is.

      To determine what the output should be, you can write a subroutine which ignores its input and returns a constant data structure which produces useful/correct output. Then you can use Data::Dumper to dump this data structure and post it.

      If you do this, I might be able to help you further. Otherwise, I wish you good luck with Brian's advice.