Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Apparent Inconsistencies in Perl Function Naming

by kael (Monk)
on Nov 26, 2000 at 02:11 UTC ( [id://43327]=note: print w/replies, xml ) Need Help??


in reply to Re: Apparent Inconsistencies in Perl Function Naming
in thread Apparent Inconsistencies in Perl Function Naming

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.

Replies are listed 'Best First'.
Re (tilly) 3: Apparent Inconsistencies in Perl Function Naming
by tilly (Archbishop) on Nov 26, 2000 at 03:53 UTC
    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...
Re: Re: Re: Apparent Inconsistencies in Perl Function Naming
by quidity (Pilgrim) on Nov 26, 2000 at 19:37 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://43327]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-19 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found