BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

I hope that this is the correct way to do this here - I replied to a reply to my original question but that appears to have been pushed down to a level where people cannot see it. Or at least I couldn't until I discovered how to changed the default setting...

If anyone has the time/inclination to try and lift the veil from my eyes - please follow this link:
Extension of original question

Additional questions:

  • Whats the easiest/quickest/most elegant way of printing out a @list one per line?
  • Is there a "headlines" view available of the fora here? I have found considerable difficulty locating answers to my previous posts!
  • Replies are listed 'Best First'.
    Re: Request for further enlightenment
    by vladb (Vicar) on Jun 05, 2002 at 06:39 UTC
      Whats the easiest/quickest/most elegant way of printing out a @list one per line?

      I believe there's already a lot of places where this question has been answered. Anyhow, excuse me if I repeat many other wise monks, but there are essentially two most common ways of accomplishing this:
      @a=qw(a b c d); # Method 1 # simply set the $, (output field separator) variable # to '\n' (new line character), and print your array # as normal. The print() method will then also print # an additional '\n' after each array element. { local $,="\n"; print @a; } print"\n----\n"; # # Here's a more straightforward way of doing this # with the join() method. # print join("\n", @a);


      _____________________
      $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}
    Re: Request for further enlightenment
    by cLive ;-) (Prior) on Jun 05, 2002 at 06:41 UTC
      1) many ways, including:
      print join "\n", @array; print "$_\n" for @array;
      2) Go to your home node, click on the number of write ups. Bookmark this page as something like "My Perlmonks contributions" if you want to make it easier to get to:) Then click on individual write up to list replies. Direct replies also appear in the chatterbox, if enabled in your user settings (don't know if it's on by default) but I don't think that replies to others' replies do - hope that makes sense :)

      cLive ;-)

      --
      seek(JOB,$$LA,0);

    Re: Request for further enlightenment
    by yodabjorn (Monk) on Jun 05, 2002 at 07:39 UTC
      Heres a good way to test:
      #!/usr/bin/perl -w use Benchmark ; use strict; my $count = shift || "-3" ; # set the test counter my @array = (); #load up an array of crap for ( my $i = 0; $i < 1000; $i++) { push @array, rand(256); } open NULL, "+/dev/null" or die "$!"; timethese ( $count, { 'join' => '&print_join', 'map' => '&print_map', 'field sep' => '&print_field' } ); # use join to print each array elemnet. sub print_join { print NULL join("\n", @array) ; } # use map to print each array elemnt, sub print_map { print NULL map{ $_ .= "\n"} @array ; } # use map to print each array elemnt, sub print_field { local $,="\n"; print NULL @array ; }
      Ran for default 3 CPU seconds:
      ./time_print 
      Benchmark: running field sep, join, map, each for at least 3 CPU seconds...
       field sep:  3 wallclock secs ( 3.15 usr +  0.00 sys =  3.15 CPU) @ 53645.71/s (n=168984)
            join:  3 wallclock secs ( 3.05 usr +  0.01 sys =  3.06 CPU) @ 1256.86/s (n=3846)
             map:  5 wallclock secs ( 4.56 usr +  0.04 sys =  4.60 CPU) @ 119.78/s (n=551)
      
      looks like the field sepperator is the fastest with 53,645.71 /sec
      hope this helps

        You are being unfair to map.

        #!/usr/bin/perl -w use Benchmark; use strict; my $count = shift || -3 ; # set the test counter my @array; #load up an array of crap push @array, rand(256) for 1 .. 1000; open NULL, ">/dev/null" or die $!; timethese ( $count, { 'join' => \&print_join, 'map' => \&print_map, 'list map' => \&print_map_list, 'field sep' => \&print_field, 'for' => \&print_for } ); # use join to print each array elemnet. sub print_join { print NULL join("\n", @array); } # use map to print each array elemnt, sub print_map { print NULL map{ $_ .= "\n"} @array; } # same but a no concat sub print_map_list { print NULL map { ($_, "\n") } @array; } # use map to print each array elemnt, sub print_field { local $, = "\n"; print NULL @array; } # use a loop. sub print_for { print NULL $_, "\n" for @array; }
        Yields:
        Benchmark: running field sep, for, join, list map, map, each for at least 9 CPU seconds...
         field sep: 11 wallclock secs ( 9.60 usr +  0.05 sys =  9.65 CPU) @ 1121.04/s (n=10818)
        
               for: 11 wallclock secs ( 9.28 usr +  0.02 sys =  9.30 CPU) @ 621.40/s (n=5779)
              join: 11 wallclock secs ( 9.20 usr +  0.02 sys =  9.22 CPU) @ 2888.07/s (n=26628)
          list map: 11 wallclock secs ( 9.07 usr +  0.04 sys =  9.11 CPU) @ 268.94/s (n=2450)
               map: 32 wallclock secs (26.02 usr +  0.26 sys = 26.28 CPU) @ 57.95/s (n=1523)
        
        What happened with your field sep test? I can't get it as fast as you claim it to be. You should also note that join does not put a newline after the final item.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.
        

          Still unfair...
            
          sub print_map_interp { print NULL map "$_\n", @array; } ### RESULTS ### Benchmark: running field sep, for, interp map, join, list map, map, ea +ch for at least 3 CPU seconds... field sep: 3 wallclock secs ( 3.09 usr + 0.05 sys = 3.14 CPU) @ 11 +76.75/s (n=3695) for: 3 wallclock secs ( 3.14 usr + 0.03 sys = 3.17 CPU) @ 81 +4.63/s (n=2584) interp map: 3 wallclock secs ( 3.09 usr + 0.02 sys = 3.11 CPU) @ 74 +5.90/s (n=2319) join: 4 wallclock secs ( 3.05 usr + 0.17 sys = 3.22 CPU) @ 36 +80.02/s (n=11846) list map: 3 wallclock secs ( 3.00 usr + 0.02 sys = 3.02 CPU) @ 44 +6.62/s (n=1347) map: 6 wallclock secs ( 6.19 usr + 0.17 sys = 6.36 CPU) @ 61 +.17/s (n=389) Tool completed successfully
             MeowChow                                   
                         s aamecha.s a..a\u$&owag.print
          I lookoed at the code for using the field sep and it tests fine. I ran the program a few more times:
           ./time_print 
          Benchmark: running field sep, join, map, each for at least 3 CPU seconds...
           field sep:  4 wallclock secs ( 3.15 usr +  0.01 sys =  3.16 CPU) @ 53475.95/s (n=168984)
                join:  4 wallclock secs ( 3.15 usr +  0.00 sys =  3.15 CPU) @ 1300.00/s (n=4095)
                 map:  3 wallclock secs ( 3.12 usr +  0.00 sys =  3.12 CPU) @ 302.88/s (n=945)
          
          
          each time on my maching the field sep is really fast.
          I changed the map to map {"$_\n"} @array for these runs