[19329] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1524 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 14 18:10:42 2001

Date: Tue, 14 Aug 2001 15:10:14 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <997827014-v10-i1524@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 14 Aug 2001     Volume: 10 Number: 1524

Today's topics:
        Not inserting duplicate elements into an array <Pcmann1@btinternet.com>
    Re: Not inserting duplicate elements into an array <jurgenex@hotmail.com>
    Re: Not inserting duplicate elements into an array <gnarinn@hotmail.com>
    Re: Not inserting duplicate elements into an array <godzilla@stomp.stomp.tokyo>
        one character at a time (Ken)
    Re: one character at a time <ilya@martynov.org>
    Re: one character at a time <cberry@cinenet.net>
    Re: perl comments (Malcolm Dew-Jones)
        Perl DBI Oracle on Linux Q <skpurcell@hotmail.com>
    Re: printing from windows (gdi32) (Mike Solomon)
        regexp gurus - help (B Puli)
    Re: regexp gurus - help <vze2r2j8@verizon.net>
        RSA Key won't save (Yiqiang Ding)
        Sorting Hash of Arrays (Drew Myers)
    Re: Sorting Hash of Arrays (Malcolm Dew-Jones)
    Re: Sorting Hash of Arrays <godzilla@stomp.stomp.tokyo>
    Re: Sorting Hash of Arrays <ren@tivoli.com>
    Re: Sorting Hash of Arrays (Tad McClellan)
        Verifying users against NT domain? <a@b.c>
    Re: Verifying users against NT domain? <ilya@martynov.org>
    Re: Way to do database read to disk instead of memory? djberge@uswest.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 14 Aug 2001 21:11:30 +0100
From: "Peter Mann" <Pcmann1@btinternet.com>
Subject: Not inserting duplicate elements into an array
Message-Id: <9lc0qd$lv0$1@plutonium.btinternet.com>

Dear All,
  I am having a problem determining how to populate an array, but if an
element already exists in the array then not insert the same element again!
For instance:



-----Here we create a new Page object which represents an HTML
page ----------
  my $page  = new Page $uri;     # Create a new Page object

------ I have called a method which returns the links from the
page -----------
  my $links = $page->links;      # Now extract the links from the page


------Now, so I can access each link, they are copied into an array, but
duplicate links may be on the same page.
------Therefore, i dont want to insert the same link twice into the array
  for my $temp (@$links) {  # Loop through all the links
    push @linkarray, $temp  # Copy all the links returned into an array
    # New line needed
  }


----- I then populate a HList widget to display all the links for that page
  my $n;
  foreach (@linkarray) {
    $hlist2->add(++$n, -text => $_);
  }



So how do I ensure duplicate links are not inserted into the array?
Any help would be much appreciated

Thanks in advance

 - Pete




------------------------------

Date: Tue, 14 Aug 2001 13:28:28 -0700
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Not inserting duplicate elements into an array
Message-Id: <3b7989eb$1@news.microsoft.com>

"Peter Mann" <Pcmann1@btinternet.com> wrote in message
news:9lc0qd$lv0$1@plutonium.btinternet.com...
> Dear All,
>   I am having a problem determining how to populate an array, but if an
> element already exists in the array then not insert the same element
again!
[...]
> So how do I ensure duplicate links are not inserted into the array?

Can you use a hash (actually the keys of a hash) instead of an array?
When you need an array just do "keys %myhash" and you got an array of all
the values you added. Of course this will not preserve the sequence used to
enter the values.

jue




------------------------------

Date: Tue, 14 Aug 2001 20:08:17 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: Not inserting duplicate elements into an array
Message-Id: <997819697.778033625800163.gnarinn@hotmail.com>

In article <9lc0qd$lv0$1@plutonium.btinternet.com>,
Peter Mann <Pcmann1@btinternet.com> wrote:

>Dear All,
>  I am having a problem determining how to populate an array, but if an
>element already exists in the array then not insert the same element again!

use a hash

gnari


------------------------------

Date: Tue, 14 Aug 2001 14:09:28 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Not inserting duplicate elements into an array
Message-Id: <3B799388.2E13742@stomp.stomp.tokyo>

Peter Mann wrote:

> I am having a problem determining how to populate an array, but if an
> element already exists in the array then not insert the same element again!
> For instance:

(snipped)


Don't you think it is rather odd so many users of
btinternet are Perl programmers and frequently
post to this newsgroup? 

Odds of this being purely coincidental are zero.


Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

@Array_Old = qw (http://la.znet.com/~callgirl
                 http://la.znet.com/~callgirl/index2.html
                 http://la.znet.com/~callgirl/rockmusi.html
                 http://la.znet.com/~callgirl/poem-idx.html);

@Array_New = qw (http://la.znet.com/~callgirl/poem-idx.html
                 http://la.znet.com/~callgirl/android.html
                 http://la.znet.com/~callgirl/chahta.cgi
                 http://la.znet.com/~callgirl/poem-idx.html
                 http://la.znet.com/~callgirl/android.html
                 http://la.znet.com/~callgirl/index2.html);


print "Old Array:\n";

for (@Array_Old)
 { print "  $_\n"; }


for (@Array_New)
 {
  $list_old = "@Array_Old";
  if (index ($list_old, $_) == -1)
   { push (@Array_Old, $_); }
 }

print "\n\nUpdated Old Array:\n";

for (@Array_Old)
 { print "  $_\n"; }


PRINTED RESULTS:
________________

Old Array:
  http://la.znet.com/~callgirl
  http://la.znet.com/~callgirl/index2.html
  http://la.znet.com/~callgirl/rockmusi.html
  http://la.znet.com/~callgirl/poem-idx.html


Updated Old Array:
  http://la.znet.com/~callgirl
  http://la.znet.com/~callgirl/index2.html
  http://la.znet.com/~callgirl/rockmusi.html
  http://la.znet.com/~callgirl/poem-idx.html
  http://la.znet.com/~callgirl/android.html
  http://la.znet.com/~callgirl/chahta.cgi


------------------------------

Date: 14 Aug 2001 11:53:20 -0700
From: kenphilbrick@mindspring.com (Ken)
Subject: one character at a time
Message-Id: <f8b445c0.0108141053.508c2ebd@posting.google.com>

I have some strings of text that I need to take one character out of
at a time.  I've looked into getc(), but that apparently only works on
filehandles (and according to the Camel book, it's also very slow).

An example of what I need:

I have the string "my little string".  I need to loop through and pull
out first the 'm', then the 'y', then the ' ', then the 'l', etc.

Thanks.

--
Ken


------------------------------

Date: 14 Aug 2001 23:00:33 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: one character at a time
Message-Id: <87d75ywjou.fsf@abra.ru>


K> I have some strings of text that I need to take one character out of
K> at a time.  I've looked into getc(), but that apparently only works on
K> filehandles (and according to the Camel book, it's also very slow).

K> An example of what I need:

K> I have the string "my little string".  I need to loop through and pull
K> out first the 'm', then the 'y', then the ' ', then the 'l', etc.

I'm sure it is not very efficient for very long strings but if you do
not care you can use following code:

my $string = "my little string";

for my $char (split '', $string) {
    # here you loop through all chars. In each iteration next char is
    # in variable $char.
}

-- 
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/)                                    |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80  E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/)                          |
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


------------------------------

Date: Tue, 14 Aug 2001 19:55:15 -0000
From: Craig Berry <cberry@cinenet.net>
Subject: Re: one character at a time
Message-Id: <Xns90FD837022507cberrycinenetnet1@207.126.101.92>

kenphilbrick@mindspring.com (Ken) wrote in
news:f8b445c0.0108141053.508c2ebd@posting.google.com: 

> I have some strings of text that I need to take one character out of
> at a time.  I've looked into getc(), but that apparently only works on
> filehandles (and according to the Camel book, it's also very slow).> 
> An example of what I need:
> 
> I have the string "my little string".  I need to loop through and pull
> out first the 'm', then the 'y', then the ' ', then the 'l', etc.

  my $str = 'my little string';

  # List technique

  foreach my $c (split '', $str) {
    print ":$c:\n";
  }

  # Substring technique

  for (my $i = 0; $ < length $str; $i++) {
    my $c = substr $str, $i, 1;
    print ":$c:\n";
  }

-- 
Craig Berry <http://www.cinenet.net/~cberry/>
"That which is now known, was once only imagined." - William Blake



------------------------------

Date: 14 Aug 2001 11:18:39 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: perl comments
Message-Id: <3b796b7f@news.victoria.tc.ca>

Eric (eric@mizuhocap.com) wrote:
: does anyone know of a way to comment out a large area of perl code other
: than placing # marks before each line?
: Something akin to the C/C++
: /* blah
:     blah
:     blah */
: //would be perfect.


One useful area is after an __END__ .  Everything after this line is
ignored.

I often put programming notes there, such as examples of things to be
parsed, or requirement notes I can read while programming, or bits of half
finished code I think I might want to use, or examples, etc. etc. etc. 
like a scratch pad area.

It's also useful while debugging, especially to find obscure syntax bugs
(the kind that cause errors much later in the script).  simly put an
__END__ part way through your script to easily check the script is valid
upto that point.  (You can have any number of __END__'s, the first one
counts.)


e.g.
	#!perl
	# example
	print "Hello...";

	__END__
	everything from here to the end of the file is a giant comment.


------------------------------

Date: Tue, 14 Aug 2001 13:21:22 -0500
From: "spurcell" <skpurcell@hotmail.com>
Subject: Perl DBI Oracle on Linux Q
Message-Id: <3b796d1f$0$4350@wodc7nh0.news.uu.net>

Hello
I have some questions about Apache w/mod_perl on Linux talking to an Oracle
DB. I was looking for a Perl/DB group, but I don't know where one would be.

Thanks
Scott






------------------------------

Date: 14 Aug 2001 13:34:16 -0700
From: mike_solomon@lineone.net (Mike Solomon)
Subject: Re: printing from windows (gdi32)
Message-Id: <56568be5.0108141234.4f74274@posting.google.com>

ebohlman@omsdev.com (Eric Bohlman) wrote in message news:<9lbaum$3u6$2@bob.news.rcn.net>...
> Mike Solomon <mike_solomon@lineone.net> wrote:
> > I am trying to write a script to print a file under a windows
> > enviroment.
>  
> > I have looked at previous messages on this subject and have taken some
> > code posted in the past have come up a script that does most of what i
> > want.
>  
> > unfortunately I can't work out how to change the orientation of the
> > printer from the script to landscape.
> 
> You need to pass the address of a DEVMODE structure as the fourth 
> parameter to CreateDC, and set its dmOrientation field to 
> DMORIENT_LANDSCAPE.

Eric,

I am sure you are right

Unfortunately I don't have a clue how to do it !!

Any help on the code would be really appreciated

Thanks

Regards


Mike Solomon


------------------------------

Date: 14 Aug 2001 11:40:37 -0700
From: bpuli@hotmail.com (B Puli)
Subject: regexp gurus - help
Message-Id: <42ec637b.0108141040.415a3abf@posting.google.com>

hi folks:

i need to remove the RCS $Log$ history form source files.

Example:

/*********************************************************************
* Header comments
**********************************************************************
* <pre>
*
* @filename    bp.c
*
* @purpose     *
* @component   *
* @comments    none
*
* @create      *
* @author      * 
* @end
*             
* </pre>
**********************************************************************/

/**************************@null{*************************************
                     Change History
 *******************************}*************************************}
 $Log: path\to\file\bp.c  $
 Revision 1.1.1.7 2001/07/02 14:52:29EDT bp
 Revision 1.1.1.6 2001/06/29 19:32:53EDT bp
 Revision 1.1.1.5 2001/06/28 19:13:25EDT bp
 Revision 1.1.1.4 2001/06/28 19:11:25EDT bp
 Revision 1.1.1.3 2001/06/28 19:10:01EDT bp
 Revision 1.1.1.2 2001/06/27 19:50:00EDT bp
 Revision 1.1.1.1 2001/06/14 18:17:07EDT bp
 Initial revision
 Revision 1.1 2001/05/08 19:48:08EDT bp
 Initial revision
 **********************************************************************/

#include "common.h"
#include "platform.h"

 ....
 ....
/* Other comments */
 ...
 ...

I am having problems writing a regexp to just get rid of the change
history comments and leave everything else.

Any help in this matter 

thanks in advance

bp


------------------------------

Date: Tue, 14 Aug 2001 21:30:29 GMT
From: "Kurt Stephens" <vze2r2j8@verizon.net>
Subject: Re: regexp gurus - help
Message-Id: <VLge7.1718$cf.385956@typhoon2.gnilink.net>

"B Puli" <bpuli@hotmail.com> wrote in message
news:42ec637b.0108141040.415a3abf@posting.google.com...
> hi folks:
>
> i need to remove the RCS $Log$ history form source files.
>
> Example:
>
> /*********************************************************************
> * Header comments
> **********************************************************************
> * <pre>
> *
> * @filename    bp.c
> *
> * @purpose     *
> * @component   *
> * @comments    none
> *
> * @create      *
> * @author      *
> * @end
> *
> * </pre>
> **********************************************************************/
>
> /*********************************************************************
>                      Change History
>  *********************************************************************
>  $Log: path\to\file\bp.c  $
>  Revision 1.1.1.7 2001/07/02 14:52:29EDT bp
>  Revision 1.1.1.6 2001/06/29 19:32:53EDT bp
>  Revision 1.1.1.5 2001/06/28 19:13:25EDT bp
>  Revision 1.1.1.4 2001/06/28 19:11:25EDT bp
>  Revision 1.1.1.3 2001/06/28 19:10:01EDT bp
>  Revision 1.1.1.2 2001/06/27 19:50:00EDT bp
>  Revision 1.1.1.1 2001/06/14 18:17:07EDT bp
>  Initial revision
>  Revision 1.1 2001/05/08 19:48:08EDT bp
>  Initial revision
>  **********************************************************************/
>
> #include "common.h"
> #include "platform.h"
>
> ....
> ....
> /* Other comments */
> ...
> ...
>
> I am having problems writing a regexp to just get rid of the change
> history comments and leave everything else.

Consider the usual idiom for stripping C comments:

[Begin code]

my $comment_re = qr'
    /\*             # Start comment

    (?:             # Match group
        [^*]        #   anything but *
    |               # or
        \*          #   *
        (?!/)       #   not followed by /
    )*              # zero or more times

    \*/             # End Comment
'x;

$code =~ s/$comment_re//;

[End code]

Or, if you prefer...

$code =~ s/\/\*(?:[^*]|\*(?!\/))*\*\///sg;


Note that the cluster (?:[^*]|\*(?!\/))* matches the text inside the
comment.  To match only RCS comments, we can set up the regex to match the
following:

Start comment
    Comment text
    RCS text
    More comment text
End comment

For a simple solution, we can match any comments that contain the text
'$Log:'.  For a more robust solution, we can restrict the RCS text match to
a line starting with optional whitespace, $Log:, any text excluding '$',
followed by '$'.  We need the multiline 'm' modifier so that ^ will match
after newlines within the text.

The following code should do the trick:

[Begin code]

my $rcs_re = qr'
    /\*             # Start comment

    (?:             # Match group
        [^*]        #   anything but *
    |               # or
        \*          #   *
        (?!/)       #   not followed by /
    )*              # zero or more times

    ^               # Start RCS line
    [ \t]*          # optional whitespace
    \$Log:          # literal $Log:
    [^$\n]+         # anything but $, newline
    \$              # literal $

    (?:             # Match group
        [^*]        #   anything but *
    |               # or
        \*          #   *
        (?!/)       #   not followed by /
    )*              # zero or more times

    \*/             # End Comment
'mx;

$code =~ s/$rcs_re//g;

[End code]

HTH,

Kurt Stephens





------------------------------

Date: 14 Aug 2001 12:31:47 -0700
From: Ding_yiqiang@yahoo.com (Yiqiang Ding)
Subject: RSA Key won't save
Message-Id: <6c95f2e4.0108141131.15caf4b2@posting.google.com>

Hi,

I'm testing the Crypt::RSA::Key stuff as follows:

    use Crypt::RSA::Key;

    my $keychain = new Crypt::RSA::Key;
    my ($public, $private) = $keychain->generate (
                              Identity  => 'dyq <dyq@domain.com>',
                              Size      => 2048,
                              Password  => 'just do it',
                              Verbosity => 1,
                              Filename  => 'dyq'
                             ) or die $keychain->errstr();

The generated key should be save to "dyq.private" and "dyq.public" as
mentioned in the doc. But it doesn't work in my redhat system. Any
idea?

BTW, the key generation process seems longer than that of
Crypt::OpenSSL::RSA, I have no idea how this happens.

YQ


------------------------------

Date: 14 Aug 2001 11:20:21 -0700
From: bh_ent@hotmail.com (Drew Myers)
Subject: Sorting Hash of Arrays
Message-Id: <d1b6a249.0108141020.2607c758@posting.google.com>

I've gone through this newsgroup, all my Perl books, and asked the few
guys in my office that code in Perl this question, with no real
resolution.

And yes, I read the excellent paper by Larry and Uri regarding Perl
Sorts, but it was a tad over my head.

1) Is it possible to sort any field of a hash of arrays via the
Schwartzian Transform?
2) How would this be coded?

This is more for my own edification, than for any particular
application.

Here's an example.

%HoA = (
        simpsons       => [ "homer", "marge", "bart" ],
        flintstones    => [ "fred", "barney" ],
        jetsons        => [ "george", "jane", "elroy" ],
);

I'd like to sort %HoA by the first field of each of the arrays, so the
output looks something like this:

%HoA = (
        flintstones    => [ "fred", "barney" ],
        jetsons        => [ "george", "jane", "elroy" ],
        simpsons       => [ "homer", "marge", "bart" ],
);

Any ideas?


------------------------------

Date: 14 Aug 2001 12:13:35 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Sorting Hash of Arrays
Message-Id: <3b79785f@news.victoria.tc.ca>

Drew Myers (bh_ent@hotmail.com) wrote:
: I've gone through this newsgroup, all my Perl books, and asked the few
: guys in my office that code in Perl this question, with no real
: resolution.

: And yes, I read the excellent paper by Larry and Uri regarding Perl
: Sorts, but it was a tad over my head.

: 1) Is it possible to sort any field of a hash of arrays via the
: Schwartzian Transform?
: 2) How would this be coded?

: This is more for my own edification, than for any particular
: application.

: Here's an example.

: %HoA = (
:         simpsons       => [ "homer", "marge", "bart" ],
:         flintstones    => [ "fred", "barney" ],
:         jetsons        => [ "george", "jane", "elroy" ],
: );

: I'd like to sort %HoA by the first field of each of the arrays, so the
: output looks something like this:

: %HoA = (
:         flintstones    => [ "fred", "barney" ],
:         jetsons        => [ "george", "jane", "elroy" ],
:         simpsons       => [ "homer", "marge", "bart" ],
: );

: Any ideas?

%HoA is a hash.  It isn't possible to sort the keys of a hash, you can
only sort elements in an array. 

(Actually there is a module that adds this feature, you'll have to search
for it, but the point is that you're misunderstanding something here.)

Commonly I keep both a hash and an array, one to contain the data and the
other to list the fields I want and in the order in which I want them.


In the above you might want something like (all untested)

	@HoA = sort { $HoA{$a}->[0] <=> $HoA{$b}->[0] } 
		    keys %Hoa;

This should sort the family names based on the name of the 1st listed
first name. 

You now have two data structures, the original hash, plus a new array
containing just the keys (the family names), sorted.  (The keys are
coincidently sorted in their own natural order as well.) 

  @HoA = (
         	flintstones    ,
         	jetsons        ,
         	simpsons       ,
         );


Now you can do things like print the original hash in the desired order

	foreach $HoA (@HoA)			# read the sorted array
	{  print "family: $HoA\n";
	   foreach $member ( @{$HoA{$HoA}} )	# get data from the hash
	   { print "    member: $member\n";
	   }
	}

(Someone else will have to examine the sort with regards to
Schwartzianizing it.) 



------------------------------

Date: Tue, 14 Aug 2001 13:30:51 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Sorting Hash of Arrays
Message-Id: <3B798A7B.36D0E05F@stomp.stomp.tokyo>

Drew Myers wrote:
 
> I've gone through this newsgroup, all my Perl books, and asked the few
> guys in my office that code in Perl this question, with no real
> resolution.

There are ample examples of how to sort a hash within the same
chapter of this book from which you borrowed your example. These
examples of how to sort literally immediately follow your quoted
example and, to discover how to sort a hash would have taken you
no more than two to three minutes to read and understand; solutions
where right under your finger tips.

 

> 1) Is it possible to sort any field of a hash of arrays via the
> Schwartzian Transform?
> 2) How would this be coded?

Have you considered writing code to discover how?

 
> Here's an example.

(snipped) 

> Any ideas?

Here is an idea. Read your book.

Another idea is your methodology is most illogical; it is
cumbersome, slow and inefficient. A hash cannot be sorted.
A hash is awkward and difficult to access. A hash is a bad
choice when an array will suffice.

Use of a hash of arrays for single dimension is a poor
programming choice compared to use of a simple old fashion
associative array. A hash should be a second choice.

Using hashes is yet another icon of Perl 5 Cargo Cultists.
They use a hash to razzle and dazzle, and cut off their
noses to spite their faces via cumbersome and slow code.


Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

%HoA = (simpsons       => [ "homer", "marge", "bart" ],
        flintstones    => [ "fred", "barney" ],
        jetsons        => [ "george", "jane", "elroy" ],);

foreach $sort (sort keys %HoA)
 { print "Key: $sort Value: @{$HoA{$sort} }\n";}

print "\n\n";

@Array = qw (simpsons:homer:marge:bart flintstones:fred:barney jetsons:george:jane:elroy);
@Array = sort (@Array);
for (@Array)
 {
   $key = substr ($_, 0, index ($_, ":"), "");
   $_ =~ tr/:/ /;
   print "Key: $key Value:$_\n";
 }

PRINTED RESULTS:
_______________

Key: flintstones Value: fred barney
Key: jetsons Value: george jane elroy
Key: simpsons Value: homer marge bart


Key: flintstones Value: fred barney
Key: jetsons Value: george jane elroy
Key: simpsons Value: homer marge bart


BENCHMARK RESULTS:
__________________

#!perl

print "Content-type: text/plain\n\n";

use Benchmark;

print "Run One:\n\n";
&Time;

print "\n\nRun Two:\n\n";
&Time;

print "\n\nRun Three:\n\n";
&Time;


sub Time
 {
  timethese (100000,
  {
   'name1' =>
   '%HoA = (simpsons       => [ "homer", "marge", "bart" ],
            flintstones    => [ "fred", "barney" ],
            jetsons        => [ "george", "jane", "elroy" ],);
     foreach $sort (sort keys %HoA)
      { $output = "Key: $sort Value: @{$HoA{$sort} }\n";}',

   'name2' =>
   '@Array = qw (simpsons:homer:marge:bart flintstones:fred:barney jetsons:george:jane:elroy);
    @Array = sort (@Array);
    for (@Array)
     {
      $key = substr ($_, 0, index ($_, ":"), "");
      $_ =~ tr/:/ /;
      $output = "Key: $key Value:$_\n";
     }',

  } );
 }


PRINTED RESULTS:
________________

Run One:

Benchmark: timing 100000 iterations of name1, name2...
 name1:  7 wallclock secs ( 6.87 usr +  0.00 sys =  6.87 CPU) @ 14556.04/s
 name2:  4 wallclock secs ( 5.27 usr +  0.00 sys =  5.27 CPU) @ 18975.33/s


Run Two:

Benchmark: timing 100000 iterations of name1, name2...
 name1:  7 wallclock secs ( 6.97 usr +  0.00 sys =  6.97 CPU) @ 14347.20/s
 name2:  6 wallclock secs ( 5.28 usr +  0.00 sys =  5.28 CPU) @ 18939.39/s


Run Three:

Benchmark: timing 100000 iterations of name1, name2...
 name1:  7 wallclock secs ( 7.03 usr +  0.00 sys =  7.03 CPU) @ 14224.75/s
 name2:  5 wallclock secs ( 5.27 usr +  0.00 sys =  5.27 CPU) @ 18975.33/s


------------------------------

Date: 14 Aug 2001 15:04:06 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Sorting Hash of Arrays
Message-Id: <m3vgjq5ryh.fsf@dhcp9-161.support.tivoli.com>

On 14 Aug 2001, bh_ent@hotmail.com wrote:

> I've gone through this newsgroup, all my Perl books, and asked the
> few guys in my office that code in Perl this question, with no real
> resolution.

Did you happen to check the Perl FAQ?  Try:

  perldoc -q sort

The two entries, "How do I sort an array by (anything)?" and "How do I
sort a hash (optionally by value instead of key)?" cover this pretty
well.

> And yes, I read the excellent paper by Larry and Uri regarding Perl
> Sorts, but it was a tad over my head.

Yes... it presupposes the above understanding, IIRC.

> 1) Is it possible to sort any field of a hash of arrays via the
> Schwartzian Transform?

Yes.

> 2) How would this be coded?

See below.

> This is more for my own edification, than for any particular
> application.
> 
> Here's an example.
> 
> %HoA = (
>         simpsons       => [ "homer", "marge", "bart" ],
>         flintstones    => [ "fred", "barney" ],
>         jetsons        => [ "george", "jane", "elroy" ],
> );
> 
> I'd like to sort %HoA by the first field of each of the arrays, so
> the output looks something like this:
> 
> %HoA = (
>         flintstones    => [ "fred", "barney" ],
>         jetsons        => [ "george", "jane", "elroy" ],
>         simpsons       => [ "homer", "marge", "bart" ],
> );

FWIW, this example isn't particularly clear as the result is also
sorted by the keys ("flintstones", "jetsons", "simpsons").

> Any ideas?

When constructing a ST (or GRT), the first step is to clearly
understand what the naive sort function would be.  In this case, that
would be:

  print "$_ => [ @{$HoA{$_}} ]\n"
    for sort { $HoA{$a}[0] cmp $HoA{$b}[0] } keys %HoA;

In all honesty, hash and array lookups are probably quick enough that
this solution is sufficient for most cases.  But since this is just a
learning example, we'll continue.  (In particular, a ST is going to
save the hash lookup, but not the array lookup, so the savings are
going to be pretty small.  A GRT may fare better.)

The basic ST model is (from the FAQ):

       @sorted = map  { $_->[0] }
                 sort { $a->[1] cmp $b->[1] }
                 map  { [ $_, uc( (/\d+\s*(\S+)/)[0]) ] } @data;

The two pieces of this that vary are the "cmp", which sometimes need
to be "<=>" instead, and the "uc(...)", which is the portion that
captures the actual sort keys.

There is one other change I like to make, though I have never tested
its usefulness.  That is to switch the order of the two elements of
the anonymous array.  It just seems like there should be a minuscule
performance advantage to referencing element 0 of an array, so it
makes sense to let the comparison function use that.

From our naive sort above, we know that we are going to use "cmp" and
that are key generation will be "$HoA{$_}[0]".  So that gives us:

  print "$_ => [ @{$HoA{$_}} ]\n"
    for map  { $_->[1] }
        sort { $a->[0] cmp $b->[0] }
        map  { [ $HoA{$_}[0], $_ ] } keys %HoA;

Switching from a ST to a GRT allows us to take advantage of the fact
that sort() is appreciably faster if it is allowed to use its built-in
comparison function.  To arrange for that, we need to pass a simple
list of strings to be compared to sort.  However, we need to be able
to extract the original data back out as well.  Figuring out exactly
how to do those two things for each particular need is what makes GRT
tricky.

In my case, I always forget what the trick is for handling sort keys
of arbitrary length.  So I just make the length non-arbitrary by
assigning a maximum and padding the rest of the values to that length.

  print "$_ => [ @{$HoA{$_}} ]\n"
    for map  { substr $_, 15 }
        sort
        map  { pack "a15 a*", $HoA{$_}[0], $_ } keys %HoA;

This particular GRT also has the advantage (or disadvantage) that it
uses the original keys as the tie-breaker.

Some simple benchmarking reveals that while the GRT is faster than the
ST, the naive sort is the fastest for all of the data sizes I tested,
up to 131072 keys.  After that the GRT becomes memory bound on my
system, so the naive is certainly faster then.

-- 
Ren Maddox
ren@tivoli.com


------------------------------

Date: Tue, 14 Aug 2001 16:15:40 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Sorting Hash of Arrays
Message-Id: <slrn9nj1nc.b3i.tadmc@tadmc26.august.net>

Malcolm Dew-Jones <yf110@vtn1.victoria.tc.ca> wrote:
>Drew Myers (bh_ent@hotmail.com) wrote:
>: I've gone through this newsgroup, all my Perl books, and asked the few
>: guys in my office that code in Perl this question, with no real
>: resolution.
>
>: And yes, I read the excellent paper by Larry and Uri regarding Perl
>: Sorts, but it was a tad over my head.
>
>: 1) Is it possible to sort any field of a hash of arrays via the
>: Schwartzian Transform?


You can sort arrays. You cannot force a storage order on
hash keys though.


>%HoA is a hash.  It isn't possible to sort the keys of a hash, you can
>only sort elements in an array. 
                          ^^^^^

A "list" actually:

   print "$_\n" for sort qw/larry moe curly/; # sorting, but no array...



>In the above you might want something like (all untested)
>
>	@HoA = sort { $HoA{$a}->[0] <=> $HoA{$b}->[0] } 
                            ^       ^^^       ^
                            ^       ^^^       ^
>		    keys %Hoa;


He wanted descending order, need to swap a/b.

He wants to compare strings, need "cmp" not "<=>".


>(Someone else will have to examine the sort with regards to
>Schwartzianizing it.) 


my @sorted = map  { $_->[0] }
             sort { $b->[1] cmp $a->[1] }
             map  { [ $_, $HoA{$_}[0] ] } keys %HoA;



-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Tue, 14 Aug 2001 11:15:03 -0700
From: BCC <a@b.c>
Subject: Verifying users against NT domain?
Message-Id: <3B796AA7.74C3AC08@b.c>

Hi,

I am running a fairly large perl app on a beowulf cluster using RH 6.2. 
Currently, users are verified against a local database before they can
use the system, and this works fine as there were not too many users to
maintain.

Now however, there is a need to verify ALL in-house users to use the
system (a nice sign of its popularity :).  The company is a windoze shop
however, and we are trying to figure out how to verify users directly
against the NT domain.  I took a look at the Win32 modules, but they are
only ( I think ) for windows machines.

Does anyone have any suggestions on modules, documentation or code to
verify users in the NT domain from a unix box?

Thanks!
Bryan


------------------------------

Date: 14 Aug 2001 22:32:37 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: Verifying users against NT domain?
Message-Id: <87hevawkze.fsf@abra.ru>


B> Hi,
B> I am running a fairly large perl app on a beowulf cluster using RH 6.2. 
B> Currently, users are verified against a local database before they can
B> use the system, and this works fine as there were not too many users to
B> maintain.

B> Now however, there is a need to verify ALL in-house users to use the
B> system (a nice sign of its popularity :).  The company is a windoze shop
B> however, and we are trying to figure out how to verify users directly
B> against the NT domain.  I took a look at the Win32 modules, but they are
B> only ( I think ) for windows machines.

B> Does anyone have any suggestions on modules, documentation or code to
B> verify users in the NT domain from a unix box?

Authen::Smb?

Disclaimer: I have never used this module.

-- 
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/)                                    |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80  E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/)                          |
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


------------------------------

Date: Tue, 14 Aug 2001 14:50:41 -0500
From: djberge@uswest.com
Subject: Re: Way to do database read to disk instead of memory?
Message-Id: <3B798111.2D2845AB@uswest.com>

"Jane B." wrote:

> Question:
>
> If I run a 'select' statement from Perl that retrieves thousands of
> rows into an array, that data is stored in memory (correct?)

Incorrect, if you just stick your 'fetch' in a while loop.  You'll
have 1 record in memory at a time.

# this, for example, will create a csv file with one record per row
while( my $arrayref = $sth->fetchrow_arrayref ){
   foreach my $rec(@$arrayref){ print SOMEFILE "$rec," }
   print SOMEFILE "\n";
}

Regards,

Dan


--
"Evil will always triumph because Good is *dumb*."
-- Dark Helmet, 'Spaceballs: The Movie'





------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 1524
***************************************


home help back first fref pref prev next nref lref last post