length of the longest $_ in @_

Discussion in 'Perl Misc' started by Sam, Sep 22, 2003.

  1. Sam

    Sam Guest

    Hello .... again :)
    after I search my book and the online hlep. trying to find it there is a
    built-in function to get the length of the longest item in a givin
    array. length will not do it and scalar @array will not do it. I can
    loop through the array but just wanted to ask if there is a built in
    function for it. and how would you know if there is a built-in-function
    for a task you want to do? or is it by time-experiance combo?

    thanks
     
    Sam, Sep 22, 2003
    #1
    1. Advertising

  2. Sam

    Matija Papec Guest

    On Mon, 22 Sep 2003 08:22:03 GMT, Sam <>
    wrote:

    >Hello .... again :)
    >after I search my book and the online hlep. trying to find it there is a
    > built-in function to get the length of the longest item in a givin
    >array. length will not do it and scalar @array will not do it. I can
    >loop through the array but just wanted to ask if there is a built in
    >function for it. and how would you know if there is a built-in-function
    >for a task you want to do? or is it by time-experiance combo?


    You'll have to loop through the array,
    #untested
    my $max = 0;
    $_>$max && $max=$_ for map length, @arr;


    or if you more prefer,
    for (map length, @arr) {
    $_>$max && $max=$_;
    }
     
    Matija Papec, Sep 22, 2003
    #2
    1. Advertising

  3. Sam

    Vlad Tepes Guest

    Sam <> wrote:

    > after I search my book and the online hlep. trying to find it there is
    > a built-in function to get the length of the longest item in a givin
    > array.


    No builtin, but it's quite easy to make one (this one's a little
    contorted to handle references to arrays):

    sub amax(@;\$) { # prototype to make it work like built-in (no parens)
    my $max = 0;
    map { $max = length if $max < length }
    ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_;
    return $max;
    }

    my @ary = qw( one two three four five );

    printf "Max length in array: %d\n", amax @ary;
    printf "Max length in aref : %d\n", amax \@ary;


    > length will not do it and scalar @array will not do it.


    Nope, length measures the length of a string, and scalar @array returns
    number of elements in the array.

    > and how would you know if there is a built-in-function for a task you
    > want to do? or is it by time-experiance combo?


    You have to read about the built-ins and remember them, or look them up:

    perldoc perlfunc

    Nice thing is that the functions are categorised. For example, here's
    the built-ins that applies to arrays:

    Functions for real @ARRAYs
    "pop", "push", "shift", "splice", "unshift"

    Read and re-read perlfunc.
    All experts do it from time to time.

    Cheers,
    --
    Vlad
     
    Vlad Tepes, Sep 22, 2003
    #3
  4. [This followup was posted to comp.lang.perl.misc]

    In article <>, samj2
    @austarmetro.com.au says...
    > Hello .... again :)
    > after I search my book and the online hlep. trying to find it there is a
    > built-in function to get the length of the longest item in a givin
    > array. length will not do it and scalar @array will not do it. I can
    > loop through the array but just wanted to ask if there is a built in
    > function for it. and how would you know if there is a built-in-function
    > for a task you want to do? or is it by time-experiance combo?
    >
    > thanks
    >
    >


    $maxlen = (sort { $b <=> $a } map { length $_ } @names)[0];
     
    Barry Kimelman, Sep 22, 2003
    #4
  5. On Mon, 22 Sep 2003, Vlad Tepes wrote:

    > sub amax(@;\$) { # prototype to make it work like built-in (no parens)


    What exactly is that prototype doing? And you don't need a prototype to
    get the function to work without parens -- it merely must be declared (or
    defined) before it's used.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Sep 22, 2003
    #5
  6. Sam

    Vlad Tepes Guest

    Jeff 'japhy' Pinyan <> wrote:

    > On Mon, 22 Sep 2003, Vlad Tepes wrote:
    >
    >> sub amax(\@;@) { # prototype to make it work like built-in (no parens)

    >
    > What exactly is that prototype doing? And you don't need a prototype to
    > get the function to work without parens -- it merely must be declared (or
    > defined) before it's used.


    I wanted to make the sub accept either an array reference or an array.
    A quick look at perlsub suggest that it doesn't work this way.

    I have to take a closer look at perlsub again. I haven't got time right
    now, though.

    --
    Vlad
     
    Vlad Tepes, Sep 22, 2003
    #6
  7. Sam

    Anno Siegel Guest

    Purl Gurl <> wrote in comp.lang.perl.misc:

    > #!perl
    >
    > @Array = qw (three two one);
    >
    > @Array = sort { length ($a) cmp length ($b) } @Array;
    >
    > print "Longest Element: $Array[$#Array]";
    >
    >
    > PRINTED RESULTS:
    > ________________
    >
    > Longest Element: three


    Now change the array:

    my @Array = qw (three two one hundredfourtyfive);

    It still prints "three".

    Anno
     
    Anno Siegel, Sep 22, 2003
    #7
  8. Sam

    Sam Guest

    Matija Papec wrote:
    > On Mon, 22 Sep 2003 08:22:03 GMT, Sam <>
    > wrote:
    >
    >
    >>Hello .... again :)
    >>after I search my book and the online hlep. trying to find it there is a
    >> built-in function to get the length of the longest item in a givin
    >>array. length will not do it and scalar @array will not do it. I can
    >>loop through the array but just wanted to ask if there is a built in
    >>function for it. and how would you know if there is a built-in-function
    >>for a task you want to do? or is it by time-experiance combo?

    >
    >
    > You'll have to loop through the array,
    > #untested
    > my $max = 0;
    > $_>$max && $max=$_ for map length, @arr;
    >
    >
    > or if you more prefer,
    > for (map length, @arr) {
    > $_>$max && $max=$_;
    > }
    >



    I have 10 arrays in my program and want to do this in all of them. my
    approch would be

    #to store the result of the loop in separate sclars for each array

    my ($arr1_max, $arr2_max, $arr(n)_max)=0;

    #is there a perl variable which holds a list of all the arrays in a
    #program? instead of doing this comming line

    for my $arr (\@arr1, \@arr2, \@arr(n)) {
    my $uni_max = '$','the name of the variable inside the $arr in this
    case arr1, how can I do this?','_max'; #builds a string which is the variable to hold
    the result of the inside loop
    for (map length, @{$arr}) {
    $uni_max=$_ if $_>$uni_max;
    }

    print $arr1_max;
    print $arr2_max;
    print $arr(n)_max;
     
    Sam, Sep 22, 2003
    #8
  9. Sam

    Sam Guest

    Barry Kimelman wrote:
    > [This followup was posted to comp.lang.perl.misc]
    >
    > In article <>, samj2
    > @austarmetro.com.au says...
    >
    >>Hello .... again :)
    >>after I search my book and the online hlep. trying to find it there is a
    >> built-in function to get the length of the longest item in a givin
    >>array. length will not do it and scalar @array will not do it. I can
    >>loop through the array but just wanted to ask if there is a built in
    >>function for it. and how would you know if there is a built-in-function
    >>for a task you want to do? or is it by time-experiance combo?
    >>
    >>thanks
    >>
    >>

    >
    >
    > $maxlen = (sort { $b <=> $a } map { length $_ } @names)[0];


    that is very clean, thanks alot
     
    Sam, Sep 22, 2003
    #9
  10. Sam

    Eric Bohlman Guest

    Matija Papec <> wrote in
    news::

    > or if you more prefer,
    > for (map length, @arr) {
    > $_>$max && $max=$_;


    $max=$_ if $_>$max;

    is more Perlish. Your form strikes me as writing sh in Perl.

    > }
     
    Eric Bohlman, Sep 22, 2003
    #10
  11. Sam

    Uri Guttman Guest

    >>>>> "PG" == Purl Gurl <> writes:

    >> Purl Gurl wrote:


    this is from the current post by moronzilla:

    >> > @Array = sort { length ($a) <=> length ($b) } @Array;

    ^^^

    this is from its first post in this thread:

    @Array = sort { length ($a) cmp length ($b) } @Array;
    ^^^

    PG> Would I practice deceit in this truly honest newsgroup?

    editing quoted parts of your crap code won't wash as google has the
    evidence and proof of your deceit and all your stupidity.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
     
    Uri Guttman, Sep 22, 2003
    #11
  12. Sam

    Anno Siegel Guest

    Purl Gurl <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    >
    >
    > > Purl Gurl wrote:

    >
    > > > @Array = qw (three two one);

    >
    > > > @Array = sort { length ($a) <=> length ($b) } @Array;

    >
    > > > print "Longest Element: $Array[$#Array]";

    >
    > > Now change the array:

    >
    > > my @Array = qw (three two one hundredfourtyfive);

    >
    > > It still prints "three".

    >
    > It does?
    >
    > It is "one-hundred-forty-five" for your interest.
    >
    > Would I practice deceit in this truly honest newsgroup?


    Good, you found the bug. Avoiding the mistake in the future would have
    been enough. Correcting the past by changing the quoted text was not
    required, and a bad move.

    Anno
     
    Anno Siegel, Sep 22, 2003
    #12
  13. Sam

    Matija Papec Guest

    X-Ftn-To: Eric Bohlman

    Eric Bohlman <> wrote:
    >Matija Papec <> wrote in
    >news::
    >
    >> or if you more prefer,
    >> for (map length, @arr) {
    >> $_>$max && $max=$_;

    >
    >$max=$_ if $_>$max;
    >
    >is more Perlish. Your form strikes me as writing sh in Perl.


    I'm sorry. :) Actually the former doesn't compile so it must be,
    $_>$max and $max=$_;
    or
    $max=$_ if $_>$max;
    as you pointed out.



    --
    Matija
     
    Matija Papec, Sep 22, 2003
    #13
  14. Sam

    Matija Papec Guest

    X-Ftn-To: Sam

    Sam <> wrote:
    >I have 10 arrays in my program and want to do this in all of them. my
    >approch would be
    >
    >#to store the result of the loop in separate sclars for each array
    >
    >my ($arr1_max, $arr2_max, $arr(n)_max)=0;
    >
    >#is there a perl variable which holds a list of all the arrays in a
    >#program?


    I'm afraid there is no such thing, but you can use array of arrays; I see
    you're used to references,

    #untested
    my @arrofarr = (\@arr1, \@arr2, \@arr3);
    my @max = (0) x (scalar @arrofarr);

    for my $i (0 .. $#arrofarr) {

    for (map length, @{ $arrofarr[$i] }) {
    $max[$i]=$_ if $_>$max[$i];
    }
    }
    print "Max lengths are: ", join ",", @max;

    hope this helps.


    --
    Matija
     
    Matija Papec, Sep 22, 2003
    #14
  15. Barry Kimelman wrote:
    > > $maxlen = (sort { $b <=> $a } map { length $_ } @names)[0];


    Sam wrote:
    > that is very clean, thanks alot


    Sorting the entire list of lengths just to find the max seems like
    overkill, at least if speed is an issue. The straightforward solution seems
    best, and you can even type it all on one line if you want.

    for (map length, @list){ $max = $_ if $_ > $max }

    Chief S.
     
    Chief Squawtendrawpet, Sep 22, 2003
    #15
  16. Sam

    Jay Tilton Guest

    Sam <> wrote:

    : after I search my book and the online hlep. trying to find it there is a
    : built-in function to get the length of the longest item in a givin
    : array. length will not do it and scalar @array will not do it. I can
    : loop through the array but just wanted to ask if there is a built in
    : function for it.

    No, there is not. It is a useful thing to be able to do, but not so
    useful to be worth consuming another keyword.

    You can get there very easily with two built-in functions and one
    bundled-in module.

    use List::Util 'max';
    $max = max( map length, @array );

    : and how would you know if there is a built-in-function
    : for a task you want to do?

    Habitual reading of perlfunc.

    : or is it by time-experiance combo?

    Bingo.
     
    Jay Tilton, Sep 23, 2003
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    370
  2. Joerg Schuster

    leftmost longest match (of disjunctions)

    Joerg Schuster, Dec 1, 2003, in forum: Python
    Replies:
    12
    Views:
    748
    Joerg Schuster
    Dec 3, 2003
  3. Licheng Fang
    Replies:
    32
    Views:
    1,379
    Tim Peters
    Sep 17, 2006
  4. John Howard
    Replies:
    14
    Views:
    268
    Paul Lalli
    Aug 19, 2005
  5. Csaba  Gabor

    Length of longest contiguous digits exercise

    Csaba Gabor, Oct 23, 2009, in forum: Javascript
    Replies:
    19
    Views:
    198
Loading...

Share This Page