Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: Perl list items

by robertw (Sexton)
on Jul 09, 2012 at 17:57 UTC ( #980733=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Perl list items
in thread Perl list items

Thank you for your help, but how would you guys take the first five items(numbers and letters) from this Scalar string that is @A? Can i use split for that?


Comment on Re^5: Perl list items
Re^6: Perl list items
by aaron_baugher (Deacon) on Jul 09, 2012 at 19:24 UTC

    You could, but that would be missing the point, and it appears that you're still confusing arrays and scalars. @A is not a scalar string; it is an array. Its first element, $A[0] is a string, which you created by assigning a string (the result of join) to it.

    I don't know exactly what combine() does here, but it appears to be returning an array of references to arrays. If you just want the values from the first sub-array, there's no need to join them into a string and then split them out again. Just deference the array:

    my @c = combine(5,@n); # get array of arrays my @A = @{$c[0]}; # get first sub-array

    UPDATE: Thanks to Anonymous for the precedence correction.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      my @c = combine(5,@n); # get array of arrays my @A = @$c[0]; # get first sub-array

      Because the 'precedence' (if that's the proper term here) of the  @ array dereference sigil is so high, the expression  @$c[0] attempts to dereference an array reference held in the  $c scalar (not in the  @c array) and then access the element at index 0 in the referenced array. (Update: Actually, my first thought was that  @$c[0] would look like a 'degenerate' array slice, like @x[0]; not sure why the appropriate warning was not generated.) The expression  @{ $c[0] } is what is needed in the given example.

      >perl -wMstrict -MData::Dump -le "my @c = (['x', 'y'], ['a', 'b'], ['c', 'd']); my $c = ['foo', 'bar']; ;; my @A1 = @ $c[0] ; my @A2 = @{ $c[0] }; dd \@A1; dd \@A2; " ["foo"] ["x", "y"]
        To clarify  @$c[0] means the same as  @{$c}[0] meaning dereference $c, take array slice

        So

        $ perl -e " use Data::Dump; my $c = [qw/ a b c/]; dd @$c[1,2]; " ("b", "c")

        While References quick reference lists this item explicitly (item 2), its somewhat documented in perlreftut/perlref but its not recommended

Re^6: Perl list items
by davido (Archbishop) on Jul 10, 2012 at 00:34 UTC

    @A is an array. If it has five or more items in it, and you want to get just those first five out of it, you can take a slice: my @five_only = @A[0..4];

    split is for when you want to take a single scalar (an entity starting with $, such as $A, $B, or $A[0] # an element of the array @A, or a literal), and split that single scalar value into a list of elements that can be stored in an array, or processed listwise.

    join is for taking a list (a naked list, or something stored in an @Array, for example), and converting that list into a single scalar delimited by whatever you pass as your first argument to join.

    You're really pretty close. Just spend an hour with perlintro, perldata, split, and join. No more than an hour (even if it takes longer to digest every last detail, an hour is all you need to get a comfortable overview).


    Dave

      Dave, thank you you are being very helpful, I am sorry I am quite new to perl and I don't understand it that well yet. Now when i do what you wrote:

      my @five_only = @A[0..4]; print @five_only

      I still get a list of about 300 numbers and letters does this mean I have to split since i need to make the string into independent items? I already tried that but it would not work this way unfortunately

      my @values = split(',', @A); foreach my $val (@values) { d = d + 1); }

      That would also be able to count however it seems that does not work, I tried the following just to get a new list of items that does function properly because when i requested te first five items i got a hundred numbers but that neither works since i got this response

      <code> my @values = split(',', @A); foreach my $val (@values) { push (@vals, '$vals'); } <code/>

      HASH(0x100804ed0)$vals

      thanks to all you guys for your help, I am unfortunately quite slow in understanding and I have been wanting to write this program a particular time now, that is why i might appear quite impatient, my apologies for that

      I am going to read the perlintro and the join split and others tomorrow

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-09-20 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (155 votes), past polls