Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Apparent Inconsistencies in Perl Function Naming

by Dominus (Parson)
on Nov 25, 2000 at 20:44 UTC ( #43318=note: print w/ replies, xml ) Need Help??


in reply to Apparent Inconsistencies in Perl Function Naming

I think you're right about the length() function. People are surprised all the time when length() fails to work on arrays.

Nat Torkington put in a proposal for Perl 6 that suggests that length(@array) be made to work properly instead of its bizarre current behavior, so you're certainly not the only person to have thought of this. I don't know whether Larry will decide to change it, however.

As for the slicing issue, it's tempting to ask that the positions in a string should be accessed by index as if the string were an array. You can do this in some other languages like C and Pascal where a string is an array. But it's hard to see how to arrange it in Perl, for syntactic reasons if nothing else. (Also, in Perl, a string is not an array.)

But if your suggestion were followed, it would still be inconsistent. @array[3..5] produces a list of three items. But presumably you want your string-slice to produce a single string, not a list. Then in your alternate universe, PawnPrince comes along and asks why the slice notation sometimes produces a list and sometimes an string, and why it isn't consistent. So it's not clear that there's any benefit from your suggestion anyway.


Comment on Re: Apparent Inconsistencies in Perl Function Naming
Re (tilly) 2 (expectations): Apparent Inconsistencies in Perl Function Naming
by tilly (Archbishop) on Nov 25, 2000 at 21:32 UTC
    Nat's suggestion fails to meet my expectations. Mine would be that length should work something like this:
    sub length { if (1 == @_) { return CORE::length(shift); } else { return map {CORE::length($_)} @_; } }
    IOW return a list of lengths instead of the number of things in the array. (Which I can easily get anyways.)

    Perl can't match both my expectations and princepawn's...

      IOW return a list of lengths instead of the number of things in the array. (Which I can easily get anyways.)

      Really then, shouldn't length return a list or a scalar depending on the context in which it was called? :)

      Note to all: If you don't see the humor in this, reread the thread

Re: Re: Apparent Inconsistencies in Perl Function Naming
by kael (Monk) on Nov 26, 2000 at 02:11 UTC
    ok, I was confused about length too but I did a test program up, once I realized what it's doing i said "DUH!!! Of course!"
    Here's the deal
    @a=qw(joe bob dude cool); print length @a;
    this returns 1. Why? length returns the length of a string in characters. the string you gave it was @a which is the # of elements in the array @a. in this case 4
    since 4 has a length of 1 character it returns 1. if you push the array so it's 10 elements it'll return 2.
    But I am forced to wonder, if you were asked to count all the letters on a page, this is simple. But what if you were told to count all the letters on this page and handed a book. what would you do? count the letters on the cover, on all the pages within the book, or count the number of pages. which of these is most logical?
    My mind thinks in perl all the time, and to me this is all perfectly logical and exactly how I'd expect it to be. course everyone thinks diffrently.
      I like that book analogy!

      My preference would be to check my context. If a list made sense I would return a list of the number of letters on each page. If it didn't I would add up the number of letters on each page, then add those up. So my previous piece of code becomes this:

      sub length { if (1 == @_) { return CORE::length(shift); } else { my @lengths = map {CORE::length($_)} @_; if (wantarray) { return @lengths; } else { my $tot = 0; $tot += $_ foreach @lengths; return $tot; } } }
      Note how this falls naturally out of length being the length of a string, and thinking of Perl as being list-oriented...

      That does let you get approximate lenths:

      my $length_ish = 10 ** (length(@array) -1)

      But then again, that's not the length, it's still just the number of elements in the array. This is nearly an Infrequently Asked Question but not quite.

        but all you have to do to get the number of elements is
        $length=@a
        Why do people need to complicate matters?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-27 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls