1  #!/usr/bin/perl -w
 2
 3  use strict;
 4  use List::MoreUtils qw/:all/;
 5
 6  my @ary          = qw/1 3 4 5/;
 7  my $search_value = 3;
 8
 9  if ( any { $_ == 3 } @ary ) {
10      print "At least one element of the \@ary equal $search_value\n";
11  }
12  else {
13      print "No one element of the \@ary equal $search_value\n";
14  }
15  # At least one element of the @ary equal 3
16
17  if ( all { $_ =~ /^\d+$/ } @ary ) {
18      print "All elements in \@ary are digit\n";
19  }
20  else {
21      print "Not all elements in \@ary are digit\n";
22  }
23  # All elements in @ary are digit
24
25  my $count = true { $_ < 5 } @ary;
26  print "$count elements in \@ary are less then 5\n";
27  # 3 elements in @ary are less then 5
28
29  my $first_index = firstidx { $_ == 4 } @ary;
30  print "First index of value 4 in \@ary is $first_index\n";
31  # First index of value 4 in @ary is 2
32
33  insert_after { $_ == 1 } 2 => @ary;
34  print "@ary\n";
35  # 1 2 3 4 5
36
37  my @multi = apply { $_ *= 2 } @ary;
38  print "@ary\n";
39  print "@multi\n";
40  # 1 2 3 4 5
41  # 2 4 6 8 10
42
43  my @indexes = indexes { $_ % 2 == 0 } @ary;
44  print "@indexes\n";
45  # 1 3
46
47  my %pairwise = pairwise { $a, $b } @ary, @multi;
48  for ( sort { $a <=> $b } keys %pairwise ) {
49      print "$_ => $pairwise{$_}\n";
50  }
51  # 1 => 2
52  # 2 => 4
53  # 3 => 6
54  # 4 => 8
55  # 5 => 10
56
57  my $iterator = natatime 4, (10..25);
58  while ( my @row = $iterator->() ) {
59      print "@row\n";
60  }
61  # 10 11 12 13
62  # 14 15 16 17
63  # 18 19 20 21
64  # 22 23 24 25
65
66  my @new_ary = zip @ary, @multi;
67  print "@new_ary\n";
68  # 1 2 2 4 3 6 4 8 5 10
69
70  my $scalar_return = uniq @new_ary;
71  print "There are $scalar_return unique elsements in the \@new_ary\n";
72  my @ary_unique = uniq @new_ary;
73  print "Array with unique elements is @ary_unique\n";
74  # There are 8 unique elsements in the @new_ary
75  # Array with unique elements is 1 2 4 3 6 8 5 10
76
77  my @_2003 = qw/2 4 6 2 6 2 1 7 5 3 4 2/;
78  my @_2004 = qw/4 4 5 2 7 2 1 7 4 3 3 2/;
79  my @_2005 = qw/1 3 7 4 4 2 0 8 6 2 4 1/;
80
81  $iterator = each_array(@_2003, @_2004, @_2005);
82  while ( my ($_2003,$_2004,$_2005) = $iterator->() ) {
83      print "$_2003 + $_2004 + $_2005 = ", $_2003 + $_2004 + $_2005, "\n";
84  }
85  # 2 + 4 + 1 = 7
86  # 4 + 4 + 3 = 11
87  # 6 + 5 + 7 = 18
88  # 2 + 2 + 4 = 8
89  # 6 + 7 + 4 = 17
90  # 2 + 2 + 2 = 6
91  # 1 + 1 + 0 = 2
92  # 7 + 7 + 8 = 22
93  # 5 + 4 + 6 = 15
94  # 3 + 3 + 2 = 8
95  # 4 + 3 + 4 = 11
96  # 2 + 2 + 1 = 5