Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/perl
- #===========================================================================================================#
- # S P L I C E perldoc -f splice #
- #===========================================================================================================#
- ## Removes the elements designated by OFFSET and LENGTH from an array, and replaces them with the elements of LIST, if any.
- ##
- ## @ret = splice(@arr, $offset, $length, @list); # replace from $offset to $offset + $length and replaced with @list and return with the replaced ones
- ## @ret = splice(@arr, $offset, $length); # removes from $offset to $offset + $length and return with the removed ones
- ## @ret = splice(@arr, $offset); # removes from $offset and return with the removed ones
- ## @ret = splice(@arr, 0, $offset); # removes the first $offset and return with the removed ones
- ##
- # TERMINOLOGY:
- # 1 .. 5 >> from 1 to 5
- # 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.
- use warnings;
- use strict;
- use Data::Dumper;
- my $snake = "\n\n" . '-' x 79 . "\n";
- warn $snake;
- # REPLACE = REPLACE $offset .. ($offset + $length) with @list
- {
- warn 'REPLACE my @b = splice(@arr, 2, 3, @list);'."\n\n";
- my @arr = (10 .. 15);
- my @list = qw(a b c e f);
- warn "BEFORE:".Dumper(\@arr);
- my @b = splice(@arr, 2, 3, @list);
- warn "AFTER:".Dumper(\@arr, \@b);
- }
- warn $snake;
- # CUT = REMOVES $offset .. ($offset + $length)
- {
- warn 'CUT my @b = splice(@arr, 2, 3);'."\n\n";
- my @arr = (10 .. 15);
- warn "BEFORE:".Dumper(\@arr);
- my @b = splice(@arr, 2, 3);
- warn "AFTER:".Dumper(\@arr, \@b);
- }
- warn $snake;
- # DELETE = REMOVES the N-th element from the list (== CUT one)
- {
- warn 'DELETE my @b = splice(@arr, $N, 1);'."\n\n";
- my @arr = (10 .. 15);
- my $N = 2;
- warn "BEFORE:".Dumper(\@arr);
- my @b = splice(@arr, $N, 1);
- warn "AFTER:".Dumper(\@arr, \@b);
- }
- warn $snake;
- # MULTI POP = REMOVES $N .. -1
- {
- warn 'MULTI POP my @b = splice(@arr, $N);'."\n\n";
- my @arr = (10 .. 15);
- my $N = 2;
- warn "BEFORE:".Dumper(\@arr);
- my @b = splice(@arr, $N);
- warn "AFTER:".Dumper(\@arr, \@b);
- }
- warn $snake;
- # MULTI SHIFT = REMOVES 0 .. $N
- {
- warn 'MULTI SHIFT my @b = splice(@arr, 0, $N);'."\n\n";
- my @arr = (10 .. 15);
- my $N = 2;
- warn "BEFORE:".Dumper(\@arr);
- my @b = splice(@arr, 0, $N);
- warn "AFTER:".Dumper(\@arr, \@b);
- }
- warn $snake;
- # INSERT = REPLACES 0 element after the N-th element in the list with a new elements
- {
- warn 'INSERT my @b = splice(@arr, $N, 0, @new);'."\n\n";
- my @arr = (10 .. 15);
- my $N = 3;
- my @new = qw(a b c);
- warn "BEFORE:".Dumper(\@arr);
- my @b = splice(@arr, $N, 0, @new);
- warn "AFTER:".Dumper(\@arr, \@b);
- }
- warn $snake;
- # does it look familiar?
- {
- warn "\nDoes it look familiar?\n\n";
- my @a = (10 .. 15);
- my @A = @a;
- my ($x, $y) = qw(a b);
- my $i = 4;
- my $l = '-24';
- sub show {printf("%-9s: %${l}s| %${l}s\n", $_[0]||'', "@A", "@a"); @a = @A = (10 .. 15);}
- &show('BEFORE');
- # !! 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)
- push(@A, $x, $y) ; splice(@a, @a, 0, $x, $y) ; # replace the element after the last element with the new elements
- &show('push');
- pop(@A) ; splice(@a, -1) ; # cut 1 element from the last element
- &show('pop');
- shift(@A) ; splice(@a, 0, 1) ; # cut 1 element from the 0th element
- &show('shift');
- unshift(@A, $x, $y) ; splice(@a, 0, 0, $x, $y) ; # replace the elements before the 0th element with the new elements
- &show('unshift');
- $A[$i] = $y ; splice(@a, $i, 1, $y) ; # cut $i-th element and replace with $y
- &show('$arr[$i]');
- }
- warn $snake;
- # Remove certain elements
- {
- warn "\nRemove certain elements\n\n";
- # 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 );
- my @array = qw(alpha beta gamma delta);
- warn "BEFORE:".Dumper(\@array);
- for ( my $index = $#array; $index >= 0; --$index ) {
- splice(@array, $index, 1)
- if ($array[$index] =~ /e/); # remove what has 'e' in itself
- }
- warn "AFTER:".Dumper(\@array);
- }
- warn $snake;
- warn "the end\n";
Advertisement
Add Comment
Please, Sign In to add comment