Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: I fear my code is unreadable

by BrowserUk (Patriarch)
on May 06, 2008 at 14:25 UTC ( [id://684956]=note: print w/replies, xml ) Need Help??


in reply to I fear my code is unreadable

I agree with everyone else that List::Util::max() is the way to go here, but for your readability concerns, the layout of your code can have a huge influence on its readability. A slight restructuring can allow you to loose some of the parens, and make things clearer:

use constant PADDING => 2; my $col_width = PADDING + ( sort { $b <=> $a } map { length } map { ref eq 'ARRAY' ? @$_ : $_ } %lists )[ 0 ]; ## Longest key or value

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: I fear my code is unreadable
by Boldra (Deacon) on May 06, 2008 at 15:16 UTC
    I like your formatting suggestions, although I'm not sure about " ref eq 'ARRAY' " as being more readable... Unfortunately List::Util and Text::Table aren't available on my target platforms.


    - Boldra

      Well, you could go with

      use constant PADDING => 2; my $col_width = PADDING + ( sort { $b <=> $a } map { length } keys %list, map { @$_ } values %lists )[ 0 ]; ## Longest key or value

      But in

      use constant PADDING => 2; my $col_width = PADDING + ( sort { $b <=> $a } map { length } map { ref ? @$_ : $_ } %lists )[ 0 ]; ## Longest key or value

      The line ref eq 'ARRAY' ? @$_ : $_ just says: if it($_) is a value (array) flatten it to a list, (which you already had in your own version), or if it is a key, pass it through untouched.

      That avoids having to treat the keys and values separately, (and iterate the hash twice to generate them). In doing so it reduces the noise level a little further.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I've adopted it because I saw it saves a loop iteration. The second version there is now actually broken. It works if you add parens, so: ref () ? @$_ : $_


        - Boldra

      List::Util and Text::Table aren't available on my target platforms

      What platform is that? List::Util is in the standard library. Everyone has it unless someone has gone to pathological efforts to reduce the size of the perl installation.

        Activeperl 5.60
        C:\>perl -V Summary of my perl5 (revision 5 version 6 subversion 0) configuration: Platform: osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread uname='' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef usethreads=undef use5005threads=undef useithreads=define usemultip +licity=def ine useperlio=undef d_sfio=undef uselargefiles=undef use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=u +ndef Compiler: cc='cl', optimize='-O1 -MD -DNDEBUG', gccversion= cppflags='-DWIN32' ccflags ='-O1 -MD -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_D +ES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX' stdchar='char', d_stdstdio=define, usevfork=false intsize=4, longsize=4, ptrsize=4, doublesize=8 d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize =4 alignbytes=8, usemymalloc=n, prototype=define Linker and Libraries: ld='link', ldflags ='-nologo -nodefaultlib -release -libpath:"C:\ +Perl\lib\C ORE" -machine:x86' libpth="C:\Perl\lib\CORE" libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib + comdlg32 .lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.lib uui +d.lib wsoc k32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib msvcrt. +lib libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl56.lib Dynamic Linking: dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release -l +ibpath:"C: \Perl\lib\CORE" -machine:x86' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY USE_ITHREADS PERL_IMPLICIT_CONTEX +T PERL_IMP LICIT_SYS Locally applied patches: ActivePerl Build 623 Built under MSWin32 Compiled at Dec 15 2000 16:27:07 @INC: C:/Perl/lib C:/Perl/site/lib . C:\>perl -MList::Util -e "print """Ok\n""";" Can't locate List/Util.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/ +site/lib . ). BEGIN failed--compilation aborted. C:\>


        - Boldra

Log In?
Username:
Password:

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

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

    No recent polls found