Solved

# shift ...

Posted on 2006-04-28
310 Views
Hello experts,

can anyone please tell me what the following code means ? may I have some examples as well ? thanks.

------------------------
sub Meow {
my(\$cat) = shift;
return int(\$cat + .1 * (\$cat <=> 0));
}
-------------------------
0
Question by:meow00

LVL 84

Expert Comment

perldoc -f shift
shift ARRAY
shift   Shifts the first value of the array off and returns it, short-
ening the array by 1 and moving everything down.  If there are
no elements in the array, returns the undefined value.  If
ARRAY is omitted, shifts the @_ array within the lexical scope
of subroutines and formats, and the @ARGV array at file scopes
or within the lexical scopes established by the "eval ''",
"BEGIN {}", "INIT {}", "CHECK {}", and "END {}" constructs.

do the same thing to the left end of an array that "pop" and
"push" do to the right end.

so
my(\$cat) = shift;
gets the first argument passed to the Meow sub
0

LVL 84

Expert Comment

.1 * (\$cat <=> 0)
is
-.1 if \$cat < 0
0 if \$cat == 0
.1 if \$cat > 0
so
Meow(-2.91) is -3)
Meow(-2.89) is -2)
0

LVL 1

Author Comment

sorry ... i don't understand the following part :

-----------
Meow(-2.91) is -3)
Meow(-2.89) is -2)
--------------

why ???
0

LVL 84

Accepted Solution

because .1 * (-2.91<=> 0) == -0.1
-2.91 + -0.1 == -3.01
int(-3.01) == -3

.1 * (-2.89<=> 0) == -0.1
-2.89 + -0.1 == -2.99
int(-2.99) == -2
0

## Featured Post

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…