Core Function Chop

From Sputnik Wiki
Jump to: navigation, search
Chop( <variable> )

Contents

Description

Removes and returns the last character from a string.

Parameters

variable

Can be any variable including numbers, strings, arrays.

Note - If it finds an ARRAY it will do the chop() on every element in the array (but not the keys).

It will chop elements that are numeric, string or arrays it will not mess with objects etc.

Return Value

Success: The character that was removed.

Failure: empty string.

Remarks

This modifies the string in place.

Example

Remove last character from a string

$str = "Hello";
print( "Removed '" . chop($str) . "'\n" );
print( "Value: '$str'\n" );
# Prints
# Removed 'o'
# Value: 'Hell'

Remove last character from each element in an array

$flowers = qw(roses tulips violets snowdrops);
chop($flowers);
printr $flowers;
# ARRAY
# {
#         [0] => rose
#         [1] => tulip
#         [2] => violet
#         [3] => snowdrop
# }

Remove last character from each element in an array that has keys

$flowers = qww(red roses yellow tulips white snowdrops);
chop($flowers);
printr $flowers;
# ARRAY
# {
#         [red] => rose
#         [yellow] => tulip
#         [white] => snowdrop
# }
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox