I'm sorry to say you've missed the point still. In your current code, @A contains only one element. That element contains a string that you created using join. You pass to join a list (or an array), and it returns a single scalar value that is essentially the concatenation of everything in the list you passed to it, glued together with the literal characters you specify as the first parameter to join.

So, @A contains only one element, the way your original code was written. To put it another way:

$A[0] eq "Some,value,with,commas,mixed,in"; $A[1] eq undef; # Nothing at all. $A[2] eq undef; # And same with $A[3], $A[4], and so on.

$A[0] is the first element of @A, and the only element that contains any data. If you didn't want all of your data stored in the first element of @A, then you shouldn't have used join, because that's exactly what it did. It took a perfectly good list and turned it into a long string so that you could store it in $A[0].

@values = split /,/, @A; would not work out either. That's because split doesn't want you to pass it a list as its second parameter (nor an array). If you do pass an array, it's evaluated in scalar context. You know what you get when you evaluate an array in scalar context? Nothing more than a count of how many elements are in the array. Know how many elements are in your array? One, because that's all you put there when you used join to populate the array (remember, join only returns one value, not a list).

Back to split: What happens when you split "1" on the comma character? Nothing, no split occurs, because there are no commas in the value "1". What you get as a return value is a single element, "1". So now @values has $values[0] == 1, and nothing else.

Moving on to the foreach: What happens when you iterate over a list that has a single element? You loop only one time. And on that one and only iteration, $val will be set to 1.

That part about d = d + 1 ); doesn't compile; that's not Perl, so I can't help you with it. Furthermore, I have no idea how d would relate to the rest of your code. But it doesn't matter because the steps that led up to that loop were flawed to begin with.

I can't do much more for you until you've taken the time to read perlintro, join, and split. After you've done that, I really think you will probably begin to get a sense of what is wrong in your code. And we'll be closer to speaking the same language with respect to what lists, contexts, arrays, scalars, and so on actually are.


Dave


In reply to Re^8: Perl list items by davido
in thread Perl list items by robertw

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.