Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: I fear my code is unreadable

by Boldra (Deacon)
on May 06, 2008 at 15:16 UTC ( [id://684982]=note: print w/replies, xml ) Need Help??


in reply to Re: I fear my code is unreadable
in thread I fear my code is unreadable

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

Replies are listed 'Best First'.
Re^3: I fear my code is unreadable
by BrowserUk (Patriarch) on May 06, 2008 at 15:36 UTC

    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
Re^3: I fear my code is unreadable
by doom (Deacon) on May 06, 2008 at 22:25 UTC

    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
        Yes, I see. It isn't your platform, it's the age of your perl installation. It appears that List::Util didn't make it into core until perl 5.7 and you're using 5.6 (and a very old version at that). You have my sympathies, but you really need to request that the version of perl should be upgraded. 5.10 just came out, and 5.8 has been available for ages...

        To see when a module was added to core:

        perl -MModule::CoreList -e "print Module::CoreList->first_release('Lis +t::Util'), \"\n\""
        This responds: 5.007003

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://684982]
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: (2)
As of 2024-04-26 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found