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 (Patriarch) on May 17, 2008 at 18:44 UTC | |
by carol (Beadle) on May 17, 2008 at 20:14 UTC | |
by ikegami (Patriarch) on May 17, 2008 at 20:59 UTC | |
by carol (Beadle) on May 17, 2008 at 21:59 UTC | |
by ikegami (Patriarch) on May 18, 2008 at 15:03 UTC | |
|
Re: Lists and Arrays and Boredom
by blokhead (Monsignor) on May 17, 2008 at 19:44 UTC | |
by carol (Beadle) on May 17, 2008 at 20:29 UTC | |
|
Re: Lists and Arrays and Boredom
by moritz (Cardinal) on May 18, 2008 at 14:27 UTC |