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?
In reply to Re: WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by almut
in thread WHAT IS WRONG TO PASS PARAMETER TO THE ARRAY?
by courierb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |