in reply to Re^2: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
in thread WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?

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;

Replies are listed 'Best First'.
Re^4: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by Anonymous Monk on Aug 06, 2009 at 10:21 UTC
    Dear almut. yes you are right.

    i just try to pass $incomedata in to the sub.
    which is part of cookie .
    and i save it as a sting in mysql db.
    but i know it looks like below without square brackets
    $VAR1 = { 'quantity' => 1, 'name' => 'abc1' }, { 'quantity' => 2, 'name' => 'abc2' }

    should i format it to

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

    is it a string format?
    sorry for my silly queestion .
    is it ok to pass a string to the sub ? the scirpt is taken partially from sample code of cgi::session cookbook
    http://search.cpan.org/~sherzodr/CGI-Session-3.94/Session/CookBook.pm

    sub display_cart { my ($cgi, $session) = @_; # getting the cart's contents my $cart = $session->param("CART") || []; my $total_price = 0; my $RV = q~<table><tr><th>Title</th><th>Price</th></tr>~; if ( $cart ) { for my $product ( @{$cart} ) { $total_price += $product->{price}; $RV = qq~ <tr> <td>$product->{name}</td> <td>$product->{price}</td> </tr>~; }
    .............the rest of code has been omitted from here

    i will give it a try after right after my pc is back to normal according your advice.

    thank you very much

      should i format it to

      The former is not valid Perl - it doesn't compile. The latter is valid Perl. Neither looks like a string. Is the input to your subroutine a string? Does the content of the string look like Data::Dumper output? Or is the input to your subroutine a reference to an array of references to hashes, each with quantity and name keys?

      is it a string format?

      Since I can't examine the input to your subroutine I can't tell you whether it is a string. You are the only one who has access to your subroutine input to determine what it is. If you want help, you must determine what the input is and post that information. It does no good to ask people who don't know and can't find out except by asking you.

      is it ok to pass a string to the sub?

      It is OK as long as the string contains the required information and the subroutine is written to process that information appropriately. Otherwise the subroutine will not produce the required output.







      OH.... it still return me blank screen.
      even i change my @partofcookie= @$incomingdata;

      foreach my $product ( @partofcookie) { .....blah blah..

      if i am using wrong data type.
      i shoudl not pass a string data to foreach loop?
      looks like i fall down again bfore the dawn....dawn

      help me pleseeeeeeeeeee


        i have change the data and make it reads like
        $VAR1 = [{'quantity' => 1,'name' => 'abc1' },{'quantity' => 2, 'name' +=> 'abc2'}];

        got blank screen again........
Re^4: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by Anonymous Monk on Aug 06, 2009 at 11:15 UTC
    Dear sir
    in regards to use Data::Dumper; print Dumper $incomingdata; I really do not know how to put it in as the core code of the main pro +gramme is too difficult for me..... but i have test it in this way sub { # # splits them up into stuff like "{name => 'name222',colour => 'yellow +'}" #my @split = (split /\Q},{/, $_[0]); #my $value = $split[0]; my $value = $_[0]; return($value); } then i use a template tag to print its data out it looks exactly the same as it shored in db [{'quantity' => 1,'name' => 'abc1' }, {'quantity' => 2, 'name' => 'abc2'}]