Thread ein Array als neue Elemente in ein anderes einfüge: und dabei ein anderes Element löschen (6 answers)
Opened by mordur at 2005-01-24 12:00

Crian
 2005-01-24 13:24
#51189 #51189
User since
2003-08-04
5870 Artikel
ModeratorIn
[Homepage]
user image
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
> perldoc -f splice
   splice ARRAY,OFFSET,LENGTH,LIST
   splice ARRAY,OFFSET,LENGTH
   splice ARRAY,OFFSET
   splice ARRAY
           Removes the elements designated by OFFSET and LENGTH from an
           array, and replaces them with the elements of LIST, if any. In
           list context, returns the elements removed from the array. In
           scalar context, returns the last element removed, or "undef" if
           no elements are removed. The array grows or shrinks as
           necessary. If OFFSET is negative then it starts that far from
           the end of the array. If LENGTH is omitted, removes everything
           from OFFSET onward. If LENGTH is negative, removes the elements
           from OFFSET onward except for -LENGTH elements at the end of the
           array. If both OFFSET and LENGTH are omitted, removes
           everything. If OFFSET is past the end of the array, perl issues
           a warning, and splices at the end of the array.

           The following equivalences hold (assuming "$[ == 0"):

               push(@a,$x,$y)      splice(@a,@a,0,$x,$y)
               pop(@a)             splice(@a,-1)
               shift(@a)           splice(@a,0,1)
               unshift(@a,$x,$y)   splice(@a,0,0,$x,$y)
               $a[$x] = $y         splice(@a,$x,1,$y)

           Example, assuming array lengths are passed before arrays:

               sub aeq {   # compare two list values
                   my(@a) = splice(@_,0,shift);
                   my(@b) = splice(@_,0,shift);
                   return 0 unless @a == @b;       # same len?
                   while (@a) {
                       return 0 if pop(@a) ne pop(@b);
                   }
                   return 1;
               }
               if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }


Offset ist der Index des ersten Elements, bei dem die Ersetzung beginnt. Er beginnt natürlich bei 0, es geht doch um Arrays.\n\n

<!--EDIT|Crian|1106566195-->
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite

View full thread ein Array als neue Elemente in ein anderes einfüge: und dabei ein anderes Element löschen