Advertisement
cwchen

[Perl 6] Manipulating an array.

Oct 7th, 2017
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.70 KB | None | 0 0
  1. my @arr = (1, 2, 3, 4, 5);
  2.  
  3. # Pop from the rear.
  4. my $popped = @arr.pop;
  5. @arr.elems == 4 or die "Wrong length";
  6. $popped == 5 or die "Wrong value";
  7.  
  8. # Push into the rear.
  9. @arr.push(99);
  10. @arr.elems == 5 or die "Wrong length";
  11. @arr[4] == 99 or die "Wrong value";
  12.  
  13. # Shift from the front.
  14. my $shifted = @arr.shift;
  15. @arr.elems == 4 or die "Wrong length";
  16. $shifted == 1 or die "Wrong value";
  17.  
  18. # Unshift into the front.
  19. @arr.unshift(100);
  20. @arr.elems == 5 or die "Wrong length";
  21. @arr[0] == 100 or die "Wrong value";
  22.  
  23. # Splice out from arbitrary location.
  24. my @spliced = @arr.splice(1, 3, (20, 30, 40));
  25. @spliced ~~ (2, 3, 4) or die "Wrong array";
  26. @arr ~~ (100, 20, 30, 40, 99) or die "Wrong array";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement