Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

shift ...

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
meow00
Asked:
meow00
  • 3
1 Solution
 
ozoCommented:
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.

               See also "unshift", "push", and "pop".  "shift" and "unshift"
               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
 
ozoCommented:
.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
 
meow00Author Commented:
sorry ... i don't understand the following part :

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

why ???
0
 
ozoCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now