Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^4: Perl list items

by frozenwithjoy (Priest)
on Jul 09, 2012 at 16:17 UTC ( #980716=note: print w/replies, xml ) Need Help??

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

Well, in your example, they have different names (one is plural, one is singular). I think at this point it comes down to personal preference. It might be easy to get the similar names confused, but you won't easily call one when you mean the other by simply making a $/@ typo.

Replies are listed 'Best First'.
Re^5: Perl list items
by robertw (Sexton) on Jul 09, 2012 at 17:57 UTC
    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?

      @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, 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/>


        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

      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"]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980716]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-06-18 10:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.