Advanced List Operations

#!/usr/bin/perl -w

@list  = (1,2,"foobar",3,4,"snafu");
@slice = @list[0,2,4];       #######  A slice of a list is still a list!
print "@slice\n\n";          #######  Should print: 1 foobar 4

@slice = @list[1..4];  
print "@slice\n\n";          #######  Should print: 2 foobar 3 4

@list = ("A" .. "Z");
print "@list\n";
print "@list[0,8,25]\n\n";   #######  Should print: A I Z

@list = (2..6, "A".."D",7);
print "@list\n\n";

#######  Slice of a list return value!
@list = (return_list())[0,3,5];   #######  Should print: 1 4 6
print "@list\n\n";                #######  Call to return_list must be in ()

@list = (-5..5);
print "$list[-2]\n\n";  ##  Should print 4. Negative indices count from end.

foreach $element (@list)   ######  Better than "for" loop!
{
    print $element," ";  ######  Prints -5 -4 -3 -2 -1 0 1 2 3 4 5
}

for ($index = 0; $index <= $#list; $index++)
{
    print $list[$index]," ";  ######  Yuck!  Executes slowly! 
}                             

@list1 = (2,5,7,9,23);
@list2 = (3,4,12,8,2);
@products = dot_product(@list1, @list2) if @list1 == @list2;
print "@products\n\n";    ######  Should print 6 20 84 72 46 

@rproducts = reverse(@products);
print "@rproducts\n\n";   ######  Should print 46 72 84 20 6


sub return_list
{
     return (1..10);
}


sub dot_product   ##  We'll see how to do this correctly when we cover
{                 ##  list REFERENCES!
     my (@list) = @_, @products;

     foreach $i (0 .. $#list/2)
     {
         $products[$i] = $list[$i] * $list[$i + 5];
     }
     return @products;
} 
####################  Results of Program Execution  ##################

1 foobar 4

2 foobar 3 4

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A I Z

2 3 4 5 6 A B C D 7

1 4 6

4

-5 -4 -3 -2 -1 0 1 2 3 4 5 

-5 -4 -3 -2 -1 0 1 2 3 4 5 

6 20 84 72 46

46 72 84 20 6
#########################################################################

#!/usr/bin/perl -w
#######################  Ord, Chr, and Splice ###########################

$var = "ABC";
print ord($var),"\n";       ######  Ord's only the first character (A).
print ord(chop($var)),"\n" while $var;

@list  = (65,66,67);
$str   = "";
$str .= chr(shift(@list)) while @list;   #####  $str should be ABC
print "$str\n";

@list = (1,2,3,4,5,6);
@removed = splice(@list, 3, 2, 11, 12, 13);
print "@list\n";
print "@removed\n";

splice(@list, 0, 2);
print "@list\n";

splice(@list, 2);
print "@list\n";
#########################  Execution Results Below  #######################

65      #######  Ord of ABC
67      #######  Ord of C
66      #######  Ord of B
65      #######  Ord of A 
ABC     #######  Result of chr(65).chr(66).chr(67)

1 2 3 11 12 13 6     #######  Spliced 11, 12, and 13 in place of 4 and 5.
4 5                  #######  Return value of splice = list of removed elts.
3 11 12 13 6         #######  splice(@list, 0, 2);
3 11                 #######  splice(@list, 2);  Removes from offset 2 to end!

################  Use of Reverse to Produce a Scalar  ##################
#!/usr/bin/perl -w

$var = "foobar";
$var = reverse $var; 
print "$var\n";     #######  Outputs "raboof"

@list = ("foo", "bar", "10");
$var  = reverse @list;
print "$var\n";     #######  Outputs "01raboof"

##########  Lesson??  Reversing a scalar only works in a scalar context.
##########  Reversing a LIST in a scalar context is like making a big
##########  scalar by concatenating list elements and then reversing
##########  the big scalar.