Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Lists and Arrays and Boredom

by carol (Beadle)
on May 17, 2008 at 10:52 UTC ( #687077=perlmeditation: print w/replies, xml ) Need Help??

I did a meditation on FAQ entry 'What is the difference between a list and an array?' In the process a little quiz sprang from it:

use strict; use warnings; use List::Util qw(shuffle); use Term::ReadKey; my $goodcount = 0; my %riddle = ( 'A ... has a changeable length.' => 'a', 'A ... does not have changeable length.' => 'l', 'A ... is something you can push or pop.' => 'a', 'A ... is a set of values.' => 'l', 'Some people say a ... is a value.' => 'l', 'Some people say a ... is a variable.' => 'a', 'A subroutine is passed a ... ' => 'l', 'A subroutine returns a ... ' => 'l', 'You put things into ... context.' => 'l', 'You initialize a ... with a list.' => 'a', 'You initialize an array with a ... ' => 'l', 'You "foreach()" across a ... ' => 'l', 'A "@" variable is a ... ' => 'a', 'An anonymous array is a ... ' => 'a', 'A ... in scalar context behaves like the number of elements in it.' => 'a', 'Subroutines access their arguments through the ... @_.' => 'a', '"push"/"pop"/"shift" only work on a ... ' => 'a', 'There is no such thing as a ... in scalar context.' => 'l', '$scalar = (2, 5, 7, 9); There never was a ... there at all.' => 'l', ); # # intro: # system("clear"); print 'Read the following FAQ:', $/, $/; print <DATA>; print 'Now start practicing.'; print ' Don\'t hesitate to re-read the FAQ if needed.', $/; print 'Answer each of the ', scalar keys %riddle, ' questions with \'l\' (list) or \'a\' (array).', $/, $/; ReadMode 'cbreak'; # no enter pressing # # loop through the questions: # for my $question ( shuffle keys %riddle ) { print $question, ' '; my $answer = ReadKey(0); if ( $answer eq $riddle{$question} ) { print $answer, ' RIGHT', $/; $goodcount++; } else { print $answer, ' WRONG', $/; } } # # the result: # print $/; if ( $goodcount / scalar keys %riddle > 0.6 ) { # being generous with that 0.6 print 'Wel done!', $/; } print 'Got ', $goodcount, ' out of ', scalar keys %riddle, ' good.', $/; __DATA__ 4.39: What is the difference between a list and an array? An array has a changeable length. A list does not. An array is something you can push or pop, while a list is a set of values. Some people make the distinction that a list is a value while an array is a variable. Subroutines are passed and return lists, you put things into list context, you initialize arrays with lists, and you "foreach()" across a list. "@" variables are arrays, anonymous arrays are arrays, arrays in scalar context behave like the number of elements in them, subroutines access their arguments through the array @_, and "push"/"pop"/"shift" only work on arrays. As a side note, there's no such thing as a list in scalar context. When you say $scalar = (2, 5, 7, 9); you're using the comma operator in scalar context, so it uses the scalar comma operator. There never was a list there at all! This causes the last value to be returned: 9.

As you can see I edited the sentences a bit for the hash keys. As an example: 'an' before 'array' was mapped to 'a' in order to not give the right answer away. It was boring to do. Some might even say running the quiz is also boring, but I discovered truthful boredom can be very enlightening.

Replies are listed 'Best First'.
Re: Lists and Arrays and Boredom
by ikegami (Pope) on May 17, 2008 at 18:44 UTC

    'Some people say a ... is a value.' => 'l'
    'Some people say a ... is a variable.' => 'a'

    Badly formed question (based on weak source text). Any answer is correct because the very fact of answering the question means at least one person says it.

    'You put things into ... context.' => 'l'

    s/put things into/evaluate expressions in/

    Expressions are evaluated in a context, not put into a context. For example, the expression "@array" evaluates to something based on context. It doesn't evaluate to something that's put into scalar context. (What would that something be? Can't be a list, cause there's no such thing as a list in scalar context.)

    perlsub uses the correct terminology.

    'You initialize a ... with a list.' => 'a'

    You can also initialize a list with a list.

    'You initialize an array with a ... ' => 'l'

    In theory, I find perfectly acceptable to read "@a=@b" as "initializing array @a with array @b", so both answers would be acceptable to me.

    In practice, the implementation of aassign would determine whether you can assign an array to an array.

    'You "foreach()" across a ... ' => 'l'

    In theory, that is the view that's presented.

    In practice, not necessarily. There are many different for loops. Some don't create a list (such as for (x..y) and for (@array)), and one iterates over an array (for (@array)).

    Note that you used the practical answer earlier and the theoretical answer here, which just adds to the confusion.

    'A ... in scalar context behaves like the number of elements in it.' => 'a'

    s/behaves like/evaluates to/, like above.

      Thanks for your comments. The source text was a recent FAQ posting in comp.lang.perl.misc. The maintainers will be interested in your thoughts.
      There are many different for loops. Some don't create a list (such as for (x..y) and for (@array)), and one iterates over an array (for (@array))
      Isn't the array evaluated in list context and the for loop iterated over a list?

        Isn't the array evaluated in list context and the for loop iterated over a list?

        No, it's optimized. You'll notice if you add elements to the array from within the loop, the loop will iterate over them too. That's wouldn't be possible if the array had been flattened to a list (as it is in the second program below) since changing the array would have no effect on the list.

        >perl -le"@a=qw(a b); for (@a) { push @a, 'c' if /a/; print }" a b c >perl -le"@a=qw(a b); for (@a,()) { push @a, 'c' if /a/; print }" a b

        The maintainers will be interested in your thoughts.

        I might do that for "put in context" vs "evaluated in context". The rest of the comments don't apply to the FAQ. You made bad assumptions that if the FAQ mentions something about lists, it can't be the case for arrays as well (and vice-versa).

Re: Lists and Arrays and Boredom
by blokhead (Monsignor) on May 17, 2008 at 19:44 UTC
    '$scalar = (2, 5, 7, 9); There never was a ... there at all.' => 'l',
    There's no array in this expression either! ;)

    blokhead

      No there isn't :) I wasn't meditating on the completeness or correctness of the FAQ. I was reading, re-reading, shuffling and playing with it any way I could think of. Then suddenly there was this quiz.
Re: Lists and Arrays and Boredom
by moritz (Cardinal) on May 18, 2008 at 14:27 UTC
    'A ... is a set of values.' => 'l',

    In mathematics a set cannot have multiple identical values. Both lists and arrays can.

    (As a side joke all items of a set have to be well defined. Is an undef value actually defined? ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2019-12-15 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?