Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?

by courierb (Novice)
on Aug 05, 2009 at 11:36 UTC ( [id://786065]=perlquestion: print w/replies, xml ) Need Help??

courierb has asked for the wisdom of the Perl Monks concerning the following question:

HI MONKS. GOOD EVENING.

I HAVE BEEN HEACHACHE FOR THIS PROBLEM FOR A MONTH.
COULD ANY ONE HERE HELP ME A BIT ABOUT it


When i uncomment line 5, it will retuan a blank page for me.
while it runs well if i comment on line 5


Why. since i need to get paratemter from $_[0], so i have to uncomment line 5, but it did not work for me. what happened?

by the way line 3 shows my $incomingdata = $_[0]; which is no problem at all as i has a testing print the string out as i expected .

so i suspect the problem is between line 4 or line 5. but i am unable to fix it up. could any one help me ? Many many many thankssssssssssssssssss


1 sub { # 2 my ($cgi, $session,$product,$tags,$itemID, $incomingdata); 3 my $incomingdata = $_[0]; ################# 4 my @partofcookie= ({name => 'abc1',quantity => 1},{name => 'abc2',q +uantity => 2}); 5 #my @partofcookie= ($incomingdata); 6 foreach my $product ( @partofcookie) { ##test 7 $product->{prod_subtotal} = $product->{price} * $product->{quantity} +; 8 $tags->{total_price} += $product->{prod_subtotal}; 9. $tags->{itemmaxtemp}=$itemmaxtemp; 10. $tags->{itemID}=$itemID; 11 push @{$tags->{cart_loop}}, $product; 12 } 13 my @xxx; 14 @xxx = @{$tags->{cart_loop}}; 15 return { orderdetail_loop => \@xxx}; 16 }

Replies are listed 'Best First'.
Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by Bloodnok (Vicar) on Aug 05, 2009 at 12:02 UTC
    I'm intrigued as to how on earth you manage to call the sub - it doesn't have a name and as the anon sub isn't recorded, isn't indirectly callable i.e. callable by reference !!

    Having said that, when uncommented, line 5 completely overwrites the default values for @partofcookie set-up in line 4 and it is (to me) unclear as to the argument list when the sub is called so suggesting a way out of your dilemma is, without further details, somewhat problematic.

    BTW, pls don't shout your question ;-/

    A user level that continues to overstate my experience : -))
      hi Bloodnok the sub is the partial of template module . it is called global by the tempalate developer. then i make a name for that global . that is the sub name i believe. later i could insert <%global%> into the page. and it will automatically call the sub and run it .
Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by n3toy (Hermit) on Aug 05, 2009 at 11:54 UTC

    In line 4 you are assigning an array of hashes to @partofcookie, then in line 5 you are assigning a variable to @partofcookie, overwriting your original array of hashes.

    (Also, no need to include the line numbers, see Writeup Formatting Tips)

      I disagree over the inclusion of line numbers esp. when large code samples are involved - IMO, it makes for far more readable discussion without the need for ad-hoc replication of bits of the OP code.

      BTW, Writeup Formatting Tips doesn't, AFAICT, mention line numbering ... or the exclusion/removal thereof.

      A user level that continues to overstate my experience :-))

        I was just going to edit my node and add that, Writeup Formatting Tips does not talk about line number (I just looked), but you caught my error first. Good catch! The OP did post just fine.

Re: What is wrong to pass parameter to the array?
by LanX (Saint) on Aug 05, 2009 at 11:48 UTC
    It's maybe OT, but I think you should better send in your keyboard, your caps-lock seems broken...

    Cheers Rolf

Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by ropey (Hermit) on Aug 05, 2009 at 12:02 UTC

    Why you are getting a blank page depends on what the full CGI is doing... However you really need to think about what you expect you code to do.

    Your line 5 is effectively creating an one by one array with the value of $incomingdata. Thats what the parenthesis is doing (note you are overwriting the array you set in line 4 if you uncomment), your then using this array in a loop which populates your hash reference. If you only required it to work with the scalar you could get completely rid of the loop.

Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by ig (Vicar) on Aug 05, 2009 at 13:11 UTC

    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.

      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.

Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by scorpio17 (Canon) on Aug 05, 2009 at 13:21 UTC

    It looks like line 5 sets @partofcookie to $incomingdata, which should contain the first parameter passed into this subroutine ($_[0]). Line 4 forces @partofcookie to (I assume) a known good value. Uncommenting line 5 overwrites this with the "real" value. If this breaks the script, I'd guess that the value of $incomingdata is bad (try dumping it out, just as a sanity check). If this is the case, the problem lies upstream, in whatever routine is calling this sub (it's passing in a bad parameter).

    By the way - you're using values from a cookie in your shopping cart logic?! Some clever hacker is going to buy 1000 units for $0.01...

      ### By the way - you're using values from a cookie in your shopping cart logic?! Some clever hacker is going to buy 1000 units for $0.01... ??? #### thanks for the reminder. i will do some code for blocking abuse
Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by ikegami (Patriarch) on Aug 05, 2009 at 16:18 UTC

    Are you trying to do

    push @partofcookie, $incomingdata;

    Your code is unreadable and it's not runnable.

      hi monks. yes. uncomment line 5 means overwrite line 4's code. acttly line 5's code what i needed. i need it to pass in the new value. line4's code is only for testing only.

      the pamameter passed into @incomedata is a dynamic data from a loop out side of the sub. i have tested. that data has been passed into $incomedat sucessfully.

      but when i try to enable line 5. that mean i try to using the data from $incomedata. i get a blank screen. while the data of line 4 and line5 are virtually the same structure.

        courierb, may I just make a few things clear?

        Thank you. I realize you are new here, so I'll try to be gentle. First, this is an online *community*. Like a real-life community, it has a social structure, with social rules. Break those rules and you can expect... well, nothing good.

        In your post you've broken several of those rules, and many of us find this annoying.

        Let me make a few requests. If you want to be a part of this particular community, you will have to abide by The Rules. I'm sorry, there is no other way.

        • Don't write in all capitals. Ever. It's hard to read, and it's very distracting.
        • Please try to ask *clearly* for what you want. I suspect you are not a native speaker of English, so most of us will try to give some grace and allow for that
        • Also, it appears you (courierb) switch to posting anonymously part-way through your own thread. This makes it hard to follow for others - who is saying what?

        That said, thank you for posting your code. That is the single most helpful thing one can do when asking for help around here. :)

        i have tested. that data has been passed into $incomedat sucessfully.

        So what exactly does it contain? You still haven't shown the output of

        use Data::Dumper; print Dumper $incomingdata;

        If it's something like

        $VAR1 = [ { 'quantity' => 1, 'name' => 'abc1' }, { 'quantity' => 2, 'name' => 'abc2' } ];

        you probably want line 5 to read

        my @partofcookie= @$incomingdata;
Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by ssandv (Hermit) on Aug 05, 2009 at 14:48 UTC
    If you use warnings, you'd also get a clue that line 5 isn't what you want from something like
    "my" variable @partofcookie masks earlier declaration in same scope at ...
Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by almut (Canon) on Aug 06, 2009 at 11:55 UTC

    If I'm understanding correctly from what you've posted so far, you appear to have a string in $incomingdata, which contains a code fragment such as

    "{'quantity' => 1,'name' => 'abc1' },{'quantity' => 2, 'name' => + 'abc2'}" (or "[{'quantity' => 1,'name' => 'abc1' },{'quantity' => 2, 'name' => + 'abc2'}]")

    and then try to use that string more or less directly in the foreach loop.

    That isn't going to work (except if you'd eval that string... but don't do that -- for security reasons!).

    The following two snippets do something entirely different:

    (1) my $incomingdata = [{name => 'abc1',quantity => 1},{name => 'abc2',qua +ntity => 2}]; my @partofcookie = @$incomingdata; (2) my $incomingdata = "[{name => 'abc1',quantity => 1},{name => 'abc2',qu +antity => 2}]"; my @partofcookie = @$incomingdata; # doesn't work!

    In the first case, Perl parses the [{...}] as code and creates a data structure, i.e. a reference to an anonymous array holding references to anonymous hashes, and $incomingdata then holds that array reference, so you can dereference it with @$incomingdata in order to copy the data into the array @partofcookie. And everything is fine.

    In the second case, Perl parses the [{...}] as a string (which happens to contain a code fragment). You can't, however, have it evaluated and dereferenced automagically by simply writing @$incomingdata (Perl makes a clear distinction between code and text data — in contrast to shell scripting, for example, where the distinction is less clear...).

    But essentially, you appear to already have figured ou the way to approach this, which is to split up the string and create the required arrayref-of-hashrefs data structure yourself... Why don't you just do it like this?

      Dear Mr almut.
      your understanding is totally comply with mine code. i do have test the both method of yours . actually i have tested it for month. i have tried various way. but i am failed. i just cant understand why it works when i put a real fixed data in it.
Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by Anonymous Monk on Aug 05, 2009 at 11:45 UTC
    Excuse me ree, why aren't you posting under your ree acount?

      What you mean?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://786065]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found