Trying to understand sort and anonymous sub

From: Dalton Marris (dmarris_at_charter.net)
Date: 07/24/04


Date: Sat, 24 Jul 2004 08:57:56 -0400
To: beginners@perl.org

I am trying to understand the use of "sub" in the second "foreach
$daynum" loop in the code shown below. I know it's an anonymous sub,
but can't figure out why it's needed, if it indeed is. I have read the
perlref and perlsub documents, and googled for information, to no avail.

Some things I have noticed regarding the first and second "foreach
$daynum" loops:

When I step through the first "foreach $daynum" loop with Komodo, it
seems to hang on the "foreach" as each combination of $a and $b values
is sorted. Stepping continues after everything is sorted. The
resultant output file contains the following (as expected):

946684800|Holiday 1|Holiday 2
950486400|Holiday 3
954547200|Holiday 5
977702400|Holiday 7|Holiday 8

When stepping through the second "foreach $daynum" loop, it only takes
one step before it continues on, so it appears to be faster. However,
it also results in an extra key, for some reason. The resultant output
file contains the following:

946684800|Holiday 1|Holiday 2
950486400|Holiday 3
954547200|Holiday 5
977702400|Holiday 7|Holiday 8
CODE(0x198f04c)

My questions:

(1) What should the "sub" be doing?

(2) Where is the "CODE(0x198f04c)" coming from?

TIA,
Dalton

#!/usr/bin/perl -w
use strict;

my $dayentry;
my $daynum;
my $dbrow;

my %dayinfo = (
     977702400 => [ "Holiday 7",
                    "Holiday 8" ],
     950486400 => [ "Holiday 3" ],
     946684800 => [ "Holiday 1",
                    "Holiday 2" ],
     954547200 => [ "Holiday 5" ],
);

open( DBFILE, ">dbfile1.txt" ) or die("Error: could not open
\"dbfile1.txt\"\n" );

foreach $daynum (sort{$a<=>$b} keys %dayinfo ) {
     $dbrow = "$daynum";
     foreach $dayentry (@{$dayinfo{$daynum}}) {
         $dbrow = join('|',$dbrow,$dayentry);
     }
     # write new data to output file
     print( DBFILE "$dbrow\n" );
}
close( DBFILE );

open( DBFILE, ">dbfile2.txt" ) or die("Error: could not open
\"dbfile2.txt\"\n" );

foreach $daynum (sort(sub{$a<=>$b}, keys %dayinfo)) {
     $dbrow = "$daynum";
     foreach $dayentry (@{$dayinfo{$daynum}}) {
         $dbrow = join('|',$dbrow,$dayentry);
     }
     # write new data to output file
     print( DBFILE "$dbrow\n" );
}
close( DBFILE );



Relevant Pages

  • Re: Count Lines in (Huge) Text Files
    ... A few years ago, I was doing some high-throughput disk stuff and my recollection is that I found the same thing you did: larger buffers only helped up to about 8K or so, and past that any improvement was minimal. ... me that with appropriate settings for its buffer, it should perform better, since it ought to be optimized for line-based i/o. ... Assuming what's hurting you in the explicit forloop is the retrieval of the data and not the counter increment, the above should perform basically as well as a plain foreach() loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: Lost data on socket - Can we start over politely?
    ... $clsel as a single-element-holding object containing the current ... simply performing the following check on $sel instead of $clsel? ... removing elements from something from a list used for the loop is a Bad ... Doesn't that throw the foreach() loop out of sync? ...
    (comp.lang.perl.misc)
  • Re: foreach statement output
    ... > I'm still trying to get an understanding of the way a foreach loop loops. ... You can't use the loop variable as an index to your array. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: array and hash patter matching
    ... however I am struggling on how to compare the hash with an array for any ... Why are you using this foreach loop inside the while loop? ... client_list is a list of one or more host names, host addresses, ...
    (perl.beginners)