Permalink
Browse files

Add stuff about slices

  • Loading branch information...
1 parent cc2ff2d commit 0e82ec854a5571b9c8ce16dc861b6e992c445fef @petdance committed May 29, 2007
Showing with 53 additions and 1 deletion.
  1. +41 −1 s/arrays.pod
  2. +8 −0 s/how-do-i.pod
  3. +4 −0 s/regexes.pod
View
@@ -113,7 +113,47 @@ C<pop> removes from the end of an array:
my $went_home = pop @customers;
-=head2 XXX Explain array slices
+=head2 Extract sections of an array with array slices
+
+Array slices are just array accesses with multiple indices.
+
+ my @a = 'a'..'z'; # 26 letters
+
+ # a, e, i, o, u...
+ my @vowels = @a[0,4,8,14,20];
+
+ # And sometimes "y"
+ push( @vowels, $a[-2] ) if rand > .5;
+
+Note that when accessing an array slice, the sigil is C<@>, not
+C<$>, because you're returning an array, not a scalar. A common
+mistake for beginners is to access an array element with a C<@>
+sigil, not C<$>, and get back a slice, which is a list:
+
+ # WRONG: Returns a 1-element list, or 1 in scalar context
+ my $z = @a[-1];
+
+ # RIGHT: Returns a single scalar element
+ my $z = $a[-1];
+
+=head2 Assign chunks of an array with array slices
+
+You can have array slices as I<lvalue>s, or values on the left side
+of the equals sign that can be assigned to.
+
+ # Replace vowels with uppercase versions
+ @a[0,4,8,14,20] = qw( A E I O U );
+
+ # Swap first and last elements
+ @a[1,-1] = @a[-1,1];
+
+Note that the left and right slices must be of the same size. Any
+missing values on the right side of the equals sign will be replaced
+with undef.
+
+=head2 splice XXX
+
+Explain splice XXX
=head2 Process arrays easily with C<map>
View
@@ -36,4 +36,12 @@ a web page, use C<WWW::Mechanize> which handles it for you.
=head2 How do I parse XML?
+=head2 How do I know if a URL is valid?
+=head2 How do I do screen access?
+
+=head2 How do I do colors?
+
+=head2 How do I do readkey?
+
+=head2 How do I do passwords?
View
@@ -203,3 +203,7 @@ right to that position and start matching.
=head2 Handle multi-line regexes
+
+=head2 Use re => debug
+
+ -Mre=debug

0 comments on commit 0e82ec8

Please sign in to comment.