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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.