Re: Trying to understand sort and anonymous sub

From: Paul Johnson (paul_at_pjcj.net)
Date: 07/24/04


Date: Sat, 24 Jul 2004 15:39:39 +0200
To: Dalton Marris <dmarris@charter.net>

On Sat, Jul 24, 2004 at 08:57:56AM -0400, Dalton Marris wrote:

> 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?

It is a bug. It shouldn't be there.

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

The first sort is using the specified subroutine {$a<=>$b} to sort
numerically. The second sort is using the default sort subroutine
({$a cmp $b }), but also sorting the sub {$a<=>$b} along with what you
want to sort. "CODE(0x198f04c)" is what you are getting when the sub is
stringified.

In your case the <=> and cmp sorts are the same, but try changing adding
99 and 1000000000 as keys and you will see the problem.

> #!/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 );

-- 
Paul Johnson - paul@pjcj.net
http://www.pjcj.net


Relevant Pages

  • Re: Help ArrayLis wont store, and how to remove duplicates????
    ... > Hi many thanks for all the replies, however i am still getting some ... > problems My sort seems to be roking fine, as they come out as they ... however in my output file it seems as my contains check does ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sort(1)
    ... an output file, sorting by the first 3 characters on a line eg 1.2, in ... 4DOS and not using some other util like sort or gsar. ... an entire file into memory it will take too much memory and even then ... it cannot do everything and could never beat the good old Unix text processing tools. ...
    (comp.os.msdos.4dos)
  • Re: Efficiency Question: Large Arrays vs. Indexed Files on Alphas
    ... >decide to read some fields of some records into an array and ... >maintain records that will eventually be written to an output file ... saved the entire file contents in dynamic memory and the SOR$ utility ... routines to sort them, then formatted the records for the output file. ...
    (comp.os.vms)
  • Re: Need to sort a very large list of words
    ... Then pull out using ORDER BY clause and ... write to output file. ... I figure I'll write a program to sort the file. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem in nested sorts
    ... I have seen the warnings ... without any of the two critical print statements. ... ONE other thing im not comprehending from this post, given the docs on sort. ... If it is contextually an anonymous sub, ...
    (comp.lang.perl.misc)