shift ...

Posted on 2006-04-28
Last Modified: 2011-10-03
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));
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.

                   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.

      my($cat) = shift;
    gets the first argument passed to the Meow sub
    LVL 84

    Expert Comment

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

    Author Comment

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

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

    why ???
    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

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    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 (,  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…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now