Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Array slices: beyond the end/ Assigning an empty list to a Hash slice

by CountZero (Bishop)
on Mar 22, 2014 at 12:54 UTC ( [id://1079388]=note: print w/replies, xml ) Need Help??


in reply to Array slices: beyond the end/ Assigning an empty list to a Hash slice

As the Ancients already said: "ex nihilo nihil fit". From an empty list, nothing comes. As can be read in perldata:
A slice of an empty list is still an empty list.

But a slice of an empty array (or beyond the present size of the array) is a list of undefs.

So:

  • @slice = ()[1,0,2];       # @slice is empty
  • @slice = (@slice)[1,0,2]; # @slice is still empty
  • @slice = @slice[1,0,2];   # now @slice contains three undefs

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Array slices: beyond the end/ Assigning an empty list to a Hash slice
by hazylife (Monk) on Mar 22, 2014 at 16:27 UTC
    Right, it's just that the following is a bit confusing:
    @b = (1..3)[3, 4]; # "empty" slice of a non-empty list; # @b is empty, which is fine @b = (1..3)[2, 3]; # the slice is "half empty", # and yet @b now has TWO elements, not one
      I understand you find it confusing, but there is a valid reason for it.

      The basic principle is that you get a slice of exactly the length you asked it to be, padded with undefs if necessary, UNLESS the whole slice would have to be padded with undefs, in which case you get an empty list which is FALSE in any boolean test.

      Actually it is a clear application of the principle of least surprise:

      • It is the simplest way to distinguish between a slice from an empty list and a slice of a list of undefs. The former is FALSE, the latter is TRUE in a test.
      • The slice returns you exactly the number of elements you asked it to return, not some unexpected or unpredictable smaller number.

      Sometimes I like to think that the authors of Perl have thought long and hard and deep about all these issues. It is a comforting thought.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

        It is the simplest way to distinguish between a slice from an empty list and a slice of a list of undefs. The former is FALSE, the latter is TRUE in a test.

        Okay.

        The slice returns you exactly the number of elements you asked it to return, not some unexpected or unpredictable smaller number.

        False. You do get a smaller number of elements, i.e. 0, when all the slice indexes are beyond the end...as you pointed out:

        ...UNLESS the whole slice would have to be padded with undefs, in which case you get an empty list

        So I think your first point is the only valid reason.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1079388]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found