Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Schwartzian Transform and memory allocation.

by ThingFish (Beadle)
on Jun 19, 2002 at 16:30 UTC ( [id://175767]=note: print w/replies, xml ) Need Help??


in reply to Schwartzian Transform and memory allocation.

More clarification here.. I do actually use a variable as an indice, as thelenm pointed out. The column to be sorted on is passed via query string and is plugged into the sort. Ex:
@file_data = map { $_->[0] } sort { $a->[$variable] cmp $b->[$variable] } map { [ $_, (split(/\|/)) ] } @file_data;

Replies are listed 'Best First'.
Re: Schwartzian Transform and memory allocation.
by Abigail-II (Bishop) on Jun 19, 2002 at 16:40 UTC
    In that case, you could easily save some memory:
    @file_data = map { $_ -> [0]} sort { $a -> [1] cmp $b -> [1]} map {[$_ => (split /\|/) [$variable - 1]]} @file_data;
    No need to use a 759 41 element arrays if you can do it with just 2 elements!

    Abigail

      Modifying my original code as per Abigail-II's suggestion dropped memory usage an entire meg. Abigail-II++.
Re^2: Schwartzian Transform and memory allocation.
by Aristotle (Chancellor) on Jun 19, 2002 at 19:21 UTC
    Why split once for the Schwartzian Transform then again for the output?
    open FILE, "<$data_file"; my @file_data = sort { $a->[$variable-1] cmp $b->[$variable-1] }, map [ split(/\|/) ], <FILE>; close FILE; print qq(<TABLE BORDER="1">); for my $row (@file_data){ print ( qq(<TR>), map(qq(<TD>$_</TD>), @$row), qq(</TR>) ); } print qq(</TABLE>);
    ____________
    Makeshifts last the longest.
      I think the

      sort { $a->[$variable-1] cmp $b->[$variable-1] },

      line should be

      sort { $a->[$variable] cmp $b->[$variable] },

      The -1 was added by abigail to skip over the $_ that was tacked on at the beginning of the anon array. You're no longer doing that.

      --

      flounder

        No, actually, Abigail added the -1 because there was no longer an extra element to skip over. It doesn't make sense to subtract 1 to skip over an extra element, does it? The same premise holds true in my snippet as well.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

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

    No recent polls found