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

i have a

workiing script shows below(the first sub below)

. basically it handling strings passed in subroutine and make a loop which shows below. ( it is working sciprt) the string passed in is an array or sting. {name => 'name222',colour => 'yellow'} or {name => 'name222',colour => 'yellow'},{name => 'name333',colour => 'yellow333'}
sub { ### this sub works well. # 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 }; }
########################################
since its output is not so easy to be formated into a desired layout.

i try to rewrite it based on first subroutine's concpets.

here is my one. but it doese not work. i suspect it is not passed the scalar value properly to the sub or it is not split it well as when i replace it with directly. it produce a result as i expected. I have already stuck on this issue for long time . could any senoir could solve the problem or light me up for this problem?
sub { my ($cgi, $session,$product,$tags,$itemmaxtemp,$itemID, $incomingdata) +; #my @incomingdata = @_ ; #my @incomingdata = $_[0]; #my @incomingdata = (@_); #my @incomingdata = $_; my $incomingdata = split /\Q},{/, $_[0];


# i have tried all above possibility and all failed.
# have been returned a blank page.
# when i filling @partofcookie directly with
#({name => 'ddd111',quantity => 11,price => '11',colour => '11red',size => '11xxl',itemID => '1111tiem'},{quantity => '2',name => '222gmailoknow',price => '228',itemID => '222item'});
# it works well. while once i replace
#@partofcookie with the value of @incomingdata , i will be
return a blank screen. whywhy?
my @partofcookie= ({name => 'ddd111',quantity => 11,price => '11',col +our => '11red',size => '11xxl',itemID => '1111tiem'},{quantity => '2' +,name => '222gmailoknow',price => '228',itemID => '222item'}); foreach my $product ( @partofcookie ) { ##### $product->{prod_subtotal} = $product->{price} * $product->{quantity}; $tags->{total_price} += $product->{prod_subtotal}; $tags->{itemmaxtemp}=$itemmaxtemp; $tags->{itemID}=$itemID; push @{$tags->{cart_loop}}, $product; } my @xxx; @xxx = @{$tags->{cart_loop}}; return { orderdetail_loop => \@xxx}; }
###########################
i believe the possible reason is i wrongly understand what is passing in the sub? so i did not split it out correctly? could
could any one help me a bit?

Many thanks in advance











Replies are listed 'Best First'.
Re: passing string to sub and string split
by Marshall (Canon) on Aug 02, 2009 at 07:56 UTC
    You claim a "working script", but I see no evidence of that. I see no use of sub{} in your code. Pass the subroutine a list of values. These can be interpreted by subroutine as simple list or a hash (key,value) pairs. Both ways are shown below. There are ways to pass references to hashes or references to arrays, but that appears to be beyond the scope of what is required here.
    #!usr/bin/perl -w use strict; my %hash = ( 'NAME' => 'name222', 'COLOR' => 'yellow', 'SIZE' => 'big', ); Some_subroutine(%hash); sub Some_subroutine { my @list = @_; print "This shows that %hash is just a list of key,value pairs\n"; print " when passed like sub(%hash)\n"; print "list=@list\n\n"; my %hash = @_; print "This shows that assigning default list (@_) \n"; print " to %hash results in hash (key,value) assignments\n\n"; foreach my $key (keys %hash) { print "key $key \tvalue is $hash{$key}\n"; } } __END__ PRINTS: This shows that %hash is just a list of key,value pairs when passed like sub(%hash) list=SIZE big COLOR yellow NAME name222 This shows that assigning default list (SIZE big COLOR yellow NAME nam +e222) to %hash results in hash (key,value) assignments key COLOR value is yellow key SIZE value is big key NAME value is name222

      Marshall's advice can also be applied to when the OP wants to pass multiple arg hashes they can be passed as refs and looped over (avoiding the nasty splitting of args!):

      #!usr/bin/perl -w ## shamelessly copied from Marshall use strict; use Data::Dumpe qw/Dumper/; ## pass two anon hash refs to the sub Some_subroutine( { 'NAME' => 'name222', 'COLOR' => 'yellow', 'SIZE' => 'big', }, { 'NAME' => 'name444', 'COLOR' => 'yellow444', 'SIZE' => 'big444', }, ); sub Some_subroutine { ## collect the refs passed to the sub my @list = @_; ## just to see what we got print Dumper \@list; ## loop through each ref passed and process as before for my $ref (@list){ foreach my $key (keys %$ref) { print "key $key \tvalue is $ref->{$key}\n"; } } }
      Just a something something...
        This is more than Just a something something.. Your code shows a certain sophistication that you are: Getting it!.

      Multiple post... Reap me!

      Just a something something...
Re: passing string to sub and string split
by ig (Vicar) on Aug 02, 2009 at 19:02 UTC
    i believe the possible reason is i wrongly understand what is passing in the sub?

    The Data::Dumper module can be very helpful when you are uncertain what data you are dealing with. To see what arguments are being passed to your subroutine, you can do something like the following:

    use strict; use warnings; use Data::Dumper; sub mysub { print Dumper(\@_); } mysub( { name => 'ddd111', quantity => 11, price => '11', colour => '11red', size => '11xxl', itemID => '1111tiem' },{ quantity => '2', name => '222gmailoknow', price => '228', itemID => '222item' } );

    This produces

    $VAR1 = [ { 'colour' => '11red', 'quantity' => 11, 'name' => 'ddd111', 'price' => '11', 'itemID' => '1111tiem', 'size' => '11xxl' }, { 'name' => '222gmailoknow', 'quantity' => '2', 'price' => '228', 'itemID' => '222item' } ];

    From this, you can see what arguments are being passed to subroutine mysub().