bnghtz

array_and_splice.pl

Jul 8th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.77 KB | None | 0 0
  1. #! /usr/bin/perl
  2.  
  3. #===========================================================================================================#
  4. #   S P L I C E                                                                          perldoc -f splice  #
  5. #===========================================================================================================#
  6. ##  Removes the elements designated by OFFSET and LENGTH from an array, and replaces them with the elements of LIST, if any.
  7. ##  
  8. ##  @ret = splice(@arr, $offset, $length, @list);  # replace from $offset to $offset + $length and replaced with @list and return with the replaced ones
  9. ##  @ret = splice(@arr, $offset, $length);         # removes from $offset to $offset + $length and return with the removed ones
  10. ##  @ret = splice(@arr, $offset);                  # removes from $offset and return with the removed ones
  11. ##  @ret = splice(@arr, 0, $offset);               # removes the first $offset and return with the removed ones
  12. ##  
  13.  
  14. # TERMINOLOGY:
  15. #       1 .. 5 >> from 1 to 5
  16. #       we are talking about lists / arrays. So do not forget the 0th element is on the beginning of the list and -1 is at the end of the list.
  17.  
  18. use warnings;
  19. use strict;
  20. use Data::Dumper;
  21.  
  22. my $snake = "\n\n" . '-' x 79 . "\n";
  23.  
  24. warn $snake;
  25.  
  26.  
  27. # REPLACE   = REPLACE  $offset .. ($offset + $length)  with  @list
  28. {
  29.     warn 'REPLACE           my @b = splice(@arr, 2, 3, @list);'."\n\n";
  30.     my @arr = (10 .. 15);
  31.     my @list = qw(a b c e f);
  32.     warn "BEFORE:".Dumper(\@arr);
  33.     my @b = splice(@arr, 2, 3, @list);
  34.     warn "AFTER:".Dumper(\@arr, \@b);
  35. }
  36.  
  37. warn $snake;
  38.  
  39.  
  40. # CUT       = REMOVES  $offset .. ($offset + $length)
  41. {
  42.     warn 'CUT               my @b = splice(@arr, 2, 3);'."\n\n";
  43.     my @arr = (10 .. 15);
  44.     warn "BEFORE:".Dumper(\@arr);
  45.     my @b = splice(@arr, 2, 3);
  46.     warn "AFTER:".Dumper(\@arr, \@b);
  47. }
  48.  
  49. warn $snake;
  50.  
  51.  
  52. # DELETE    = REMOVES the N-th element from the list  (== CUT one)
  53. {
  54.     warn 'DELETE            my @b = splice(@arr, $N, 1);'."\n\n";
  55.     my @arr = (10 .. 15);
  56.     my $N = 2;
  57.     warn "BEFORE:".Dumper(\@arr);
  58.     my @b = splice(@arr, $N, 1);
  59.     warn "AFTER:".Dumper(\@arr, \@b);
  60. }
  61.  
  62. warn $snake;
  63.  
  64.  
  65. # MULTI POP     = REMOVES  $N .. -1
  66. {
  67.     warn 'MULTI POP         my @b = splice(@arr, $N);'."\n\n";
  68.     my @arr = (10 .. 15);
  69.     my $N = 2;
  70.     warn "BEFORE:".Dumper(\@arr);
  71.     my @b = splice(@arr, $N);
  72.     warn "AFTER:".Dumper(\@arr, \@b);
  73. }
  74.  
  75. warn $snake;
  76.  
  77.  
  78. # MULTI SHIFT   = REMOVES  0 .. $N
  79. {
  80.     warn 'MULTI SHIFT       my @b = splice(@arr, 0, $N);'."\n\n";
  81.     my @arr = (10 .. 15);
  82.     my $N = 2;
  83.     warn "BEFORE:".Dumper(\@arr);
  84.     my @b = splice(@arr, 0, $N);
  85.     warn "AFTER:".Dumper(\@arr, \@b);
  86. }
  87.  
  88. warn $snake;
  89.  
  90.  
  91. # INSERT        = REPLACES 0 element after the N-th element in the list with a new elements
  92. {
  93.     warn 'INSERT            my @b = splice(@arr, $N, 0, @new);'."\n\n";
  94.     my @arr = (10 .. 15);
  95.     my $N = 3;
  96.     my @new = qw(a b c);
  97.     warn "BEFORE:".Dumper(\@arr);
  98.     my @b = splice(@arr, $N, 0, @new);
  99.     warn "AFTER:".Dumper(\@arr, \@b);
  100. }
  101.  
  102. warn $snake;
  103.  
  104.  
  105. # does it look familiar?
  106. {
  107.     warn "\nDoes it look familiar?\n\n";
  108.    
  109.     my @a = (10 .. 15);
  110.     my @A = @a;
  111.     my ($x, $y) = qw(a b);
  112.     my $i = 4;
  113.     my $l = '-24';
  114.  
  115.     sub show {printf("%-9s: %${l}s| %${l}s\n", $_[0]||'', "@A", "@a"); @a = @A = (10 .. 15);}
  116.     &show('BEFORE');
  117.                                     # !! the second @a is considered as a SCALAR = number of the array elements - well it's ugly but works. A nice solution would be splice(@a, scalar(@a), 0, $x, $y)
  118.     push(@A, $x, $y)    ;  splice(@a, @a, 0, $x, $y) ; # replace the element after the last element with the new elements
  119.     &show('push');
  120.     pop(@A)             ;  splice(@a, -1)            ; # cut 1 element from the last element
  121.     &show('pop');
  122.     shift(@A)           ;  splice(@a, 0, 1)          ; # cut 1 element from the 0th element
  123.     &show('shift');
  124.     unshift(@A, $x, $y) ;  splice(@a, 0, 0, $x, $y)  ; # replace the elements before the 0th element with the new elements
  125.     &show('unshift');
  126.     $A[$i] = $y         ;  splice(@a, $i, 1, $y)     ; # cut $i-th element and replace with $y
  127.     &show('$arr[$i]');
  128. }  
  129.  
  130. warn $snake;
  131.  
  132.  
  133. # Remove certain elements
  134. {
  135.     warn "\nRemove certain elements\n\n";
  136.    
  137.     # my @array = qw(alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron pi rho sigma tau upsilon phi chi spi omega );
  138.     my @array = qw(alpha beta gamma delta);
  139.     warn "BEFORE:".Dumper(\@array);
  140.     for ( my $index = $#array; $index >= 0; --$index ) {
  141.         splice(@array, $index, 1)
  142.             if ($array[$index] =~ /e/);  # remove what has 'e' in itself
  143.     }
  144.     warn "AFTER:".Dumper(\@array);
  145. }
  146.  
  147. warn $snake;
  148. warn "the end\n";
Advertisement
Add Comment
Please, Sign In to add comment