perlbench

 view release on metacpan or  search on metacpan

benchmarks/app/perlfunc.pod  view on Meta::CPAN

    package other;
    sub backwards ($$) { $_[1] cmp $_[0]; }	# $a and $b are not set here

    package main;
    @new = sort other::backwards @old;

    # guarantee stability, regardless of algorithm
    use sort 'stable';
    @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;

    # force use of mergesort (not portable outside Perl 5.8)
    use sort '_mergesort';  # note discouraging _
    @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;

If you're using strict, you I<must not> declare $a
and $b as lexicals.  They are package globals.  That means
if you're in the C<main> package and type

    @articles = sort {$b <=> $a} @files;

then C<$a> and C<$b> are C<$main::a> and C<$main::b> (or C<$::a> and C<$::b>),
but if you're in the C<FooPack> package, it's the same as typing

    @articles = sort {$FooPack::b <=> $FooPack::a} @files;

The comparison function is required to behave.  If it returns
inconsistent results (sometimes saying C<$x[1]> is less than C<$x[2]> and
sometimes saying the opposite, for example) the results are not
well-defined.

Because C<< <=> >> returns C<undef> when either operand is C<NaN>
(not-a-number), and because C<sort> will trigger a fatal error unless the
result of a comparison is defined, when sorting with a comparison function
like C<< $a <=> $b >>, be careful about lists that might contain a C<NaN>.
The following example takes advantage of the fact that C<NaN != NaN> to
eliminate any C<NaN>s from the input.

    @result = sort { $a <=> $b } grep { $_ == $_ } @input;

=item splice ARRAY,OFFSET,LENGTH,LIST

=item splice ARRAY,OFFSET,LENGTH

=item splice ARRAY,OFFSET

=item 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 C<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 C<< $[ == 0 and $#a >= $i >> )

    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[$i] = $y		splice(@a,$i,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)) { ... }

=item split /PATTERN/,EXPR,LIMIT

=item split /PATTERN/,EXPR

=item split /PATTERN/

=item split

Splits the string EXPR into a list of strings and returns that list.  By
default, empty leading fields are preserved, and empty trailing ones are
deleted.  (If all fields are empty, they are considered to be trailing.)

In scalar context, returns the number of fields found and splits into
the C<@_> array.  Use of split in scalar context is deprecated, however,
because it clobbers your subroutine arguments.

If EXPR is omitted, splits the C<$_> string.  If PATTERN is also omitted,
splits on whitespace (after skipping any leading whitespace).  Anything
matching PATTERN is taken to be a delimiter separating the fields.  (Note
that the delimiter may be longer than one character.)

If LIMIT is specified and positive, it represents the maximum number
of fields the EXPR will be split into, though the actual number of
fields returned depends on the number of times PATTERN matches within
EXPR.  If LIMIT is unspecified or zero, trailing null fields are
stripped (which potential users of C<pop> would do well to remember).
If LIMIT is negative, it is treated as if an arbitrarily large LIMIT
had been specified.  Note that splitting an EXPR that evaluates to the
empty string always returns the empty list, regardless of the LIMIT
specified.

A pattern matching the null string (not to be confused with
a null pattern C<//>, which is just one member of the set of patterns
matching a null string) will split the value of EXPR into separate
characters at each point it matches that way.  For example:

    print join(':', split(/ */, 'hi there'));

produces the output 'h:i:t:h:e:r:e'.

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.301 second using v1.00-cache-2.02-grep-82fe00e-cpan-d29e8ade9f55 )