[10176] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3769 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Sep 20 23:07:30 1998

Date: Sun, 20 Sep 98 20:00:20 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 20 Sep 1998     Volume: 8 Number: 3769

Today's topics:
    Re: /etc/passwd file <mpersico@erols.com>
    Re: any way to encrypt my script? <sneaker@sneex.fccj.org>
    Re: any way to encrypt my script? <rra@stanford.edu>
    Re: C Struct -> Perl (Mark-Jason Dominus)
    Re: Checking if open dir contains a sub directory (Mark-Jason Dominus)
    Re: Code: Help() function and printing embedded POD <mpersico@erols.com>
        Could someone help me with this code!!! <krisb@users.qual.net>
    Re: Danish letters =?iso-8859-1?Q?=C6_=D8_=C5?= (Sam Holden)
        Does perl5 have a switch statement? maeda@src.ricoh.co.jp
    Re: Does perl5 have a switch statement? <sneaker@sneex.fccj.org>
    Re: Does perl5 have a switch statement? (Mark-Jason Dominus)
    Re: even/odd numbers (John Moreno)
    Re: even/odd numbers (Josh Kortbein)
        getting the pid of the process started with 'open' <ghersh@bbnplanet.com>
        How to combine hash %b to hash %a? (union) (Jari Aalto+mail.perl)
    Re: How to combine hash %b to hash %a? (union) (Martien Verbruggen)
    Re: How to combine hash %b to hash %a? (union) <rick.delaney@shaw.wave.ca>
    Re: How to combine hash %b to hash %a? (union) <mpersico@erols.com>
    Re: Number Sort <garry@america.net>
        Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)

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

Date: Sun, 20 Sep 1998 22:34:40 -0400
From: "Matthew O. Persico" <mpersico@erols.com>
Subject: Re: /etc/passwd file
Message-Id: <3605BB40.1B9A1132@erols.com>

And what makes you think you're going to get 12 angry perl hackers? More
likely, you'll get 12 angry perl newbies who've been flamed on clp.misc
and hell-bent on revenge! <grin>

Greg Bacon wrote:
> 
> In article <36032A94.D5E3DFB4@erols.com>,
>         "Matthew O. Persico" <mpersico@erols.com> writes:
> : But you better hope you don't get sued. <grin>
> 
> A jury of *my* peers wouldn't convict me if someone blindly and
> stupidly followed some advice I posted to Usenet. :-)
> 
> Greg
> --
> The most incomprehensible thing about the universe is that it is
> comprehensible.
>     -- Albert Einstein

-- 
Get "The Perl Cookbook"
Christansen and Torkington. 
O'Reilly. ISBN 1-56592-243-3
FAQ on serious steriods!


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

Date: Sun, 20 Sep 1998 21:53:51 -0400
From: Bill 'Sneex' Jones <sneaker@sneex.fccj.org>
Subject: Re: any way to encrypt my script?
Message-Id: <3605B1AF.F4B773FF@sneex.fccj.org>

bitnut1@my-dejanews.com wrote:
> 
> >------------------------------------------
> > Russ Allbery <rra@stanford.edu>:
> > ...
> > Keep in mind that you can compile Perl to a machine-readable binary
> > executable if you want, provided you don't do too many cute module tricks.
> >------------------------------------------
> 
> The Perl "compiled executable" looks a bit funny:
> -- A 2-line script results in a 950Kb "executable".
> -- The variable names are embedded in a human readable form (?!).
> -- The execution speed is close to the non-compiled form, etc.
> 
> You can call it a duck, but it looks, smells and walks like
> interpreted code.

Hmmm, my 41 byte C program became 4,200 bytes...

So you don't have a valid argument.

You can call it interpreted, but if it works the sames,
inputs the same, outputs the same - I call it progress.

'Compiled' code is becoming real old real fast.
Hell, just ask the 4GL people, like Progress, that
code looks compiled but it's not.

Just because a 'development environment' keeps it's
'compiled' code in memory at runtime doesn't make
it less effective than one which sticks it out on disk
for you to poke around with.

Besides, have you ported any good 'portable' C lately?


-Sneex-  (who's not happy unless he's stokin' the fires)  :]
__________________________________________________________________
Bill Jones | FCCJ Webmaster | http://webmaster.fccj.org/Webmaster
__________________________________________________________________
We are the CLPM... Lower your standards and surrender your code...
We will add your biological and technological distinctiveness to 
our own... Your thoughts will adapt to service us...
 ...Resistance is futile...


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

Date: 20 Sep 1998 19:16:55 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: any way to encrypt my script?
Message-Id: <yl7lyyjjl4.fsf@windlord.stanford.edu>

Bill 'Sneex' Jones <sneaker@sneex.fccj.org> writes:

> 'Compiled' code is becoming real old real fast.  Hell, just ask the 4GL
> people, like Progress, that code looks compiled but it's not.

Some people would argue the same is true of ordinary Perl....  ;)

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 21 Sep 1998 02:23:56 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: C Struct -> Perl
Message-Id: <6u4dbs$osh$1@netnews.upenn.edu>

In article <6u11tk$2gt$1@news.dn.net>, Andy <rapruitt@inetconn.net> wrote:
>I'm trying to replicate a C struct  in Perl and I'm having a bad time of it.

It looks to me like you have the wrong padding.  Do this:

	struct CUST {
	   char start_code[5];
	   int rc;
	   int slength;
	};

	#include <stdio.h>
	int main(void) {
	  struct CUST s;
	  int offset_start_code = (char*)(&s.start_code) - (char*)&s;
	  int offset_rc = (char*)(&s.rc) - (char*)&s;
	  int offset_slength = (char*)(&s.slength) - (char*)&s;
	  int size_start_code = sizeof(s.start_code);
	  int size_rc = sizeof(s.rc);
	  int size_slength = sizeof(s.slength);
	  printf("start_code: %d bytes at offset %d.\n", size_start_code, offset_start_code);
	  printf("rc: %d bytes at offset %d.\n", size_rc, offset_rc);
	  printf("slength: %d bytes at offset %d.\n", size_slength, offset_slength);
	  printf("Total struct size: %d.\n", sizeof(s));
	  return 0;
	}

On my machine, this prints:

	start_code: 5 bytes at offset 0.
	rc: 4 bytes at offset 8.
	slength: 4 bytes at offset 12.
	Total struct size: 16.

Which means that the layout is like this:

	0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 |
	|start_code    |xxxxxxxx|rc        |slength    |

That means that your pack template should look like this:

	$struct = pack "a5 x3 N N", 'start', $rc, $slength;
	# Struct is now 16 bytes in the format depicted above.
	
That is all you need to do.  `join' is unnecessary.

>Does join preserve the byte order and word boundaries? 

Join preseves all the orders.  All it does is concatenate things.  As
for `word boundaries', your question makes no sense.  Do you know what
word boundaries are?

> Is specifying the null bytes in my pack sufficient?

Yes, but you specified far too many of them.

>Are these snippets equivalent?

Probably not, but you should run the program I showed to be sure.


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

Date: 21 Sep 1998 01:23:58 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: Checking if open dir contains a sub directory
Message-Id: <6u49re$7gs$1@netnews.upenn.edu>


In article <3605878E.862715E3@america.net>,
Garry T. Williams <garry@america.net> wrote:
>Be that as it may, here's how you could use
>the Perl -d operator to solve your problem: 

Too much code.  Try this:

	use File::Find;
	find(\&rmr, $top);

	sub rmr {
	  return if -d $_ && ! -l _;
	  unlink $_ or warn "Couldn't remove $File::Find::name: $!\n";
	}



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

Date: Sun, 20 Sep 1998 22:44:15 -0400
From: "Matthew O. Persico" <mpersico@erols.com>
Subject: Re: Code: Help() function and printing embedded POD
Message-Id: <3605BD7F.9B8497BC@erols.com>

Hi Jari!
Long time, no bytes <grin>.

Anyway, You may need another way to get the perl interpreter. I don't
think she-bang works under DOS, therefore I would not expect it to be
there. When I ran the pod2text on this file, it told me there was no
pod. Strange.

-- 
Get "The Perl Cookbook"
Christansen and Torkington. 
O'Reilly. ISBN 1-56592-243-3
FAQ on serious steriods!


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

Date: Mon, 21 Sep 1998 01:53:09 GMT
From: Kris Bernardic <krisb@users.qual.net>
Subject: Could someone help me with this code!!!
Message-Id: <3605DBF4.69463257@users.qual.net>

I'm having a helluva time trying to figure out what is wrong with this
script (why the server is giving me an error).  I've bolded below where
I think the problem is.  How this script works is a user enters a login
name (name=logi) and a password (name=pass) and if everything checkout
on the 'password.file' they are transported to a page either
member_index.html or member_low_graphics.html.  I have a checkbox setup
that the user checks if they want the low graphic page (as opposed to
the graphic intensive default page).  The checkbox is (name=version
value=1).  So I'm not sure if I have the elseif part right, or what.
Any help would be greatly appreciated because I've spent quite awhile on
this to no avail.

Kris


#!/usr/bin/perl

$AUTHFILE = "password.file";
#Here you must write a real password file.
#Structure of this file very simple
#login1=password1
#login2=password2
#and so on.......


print "Content-type: text/html\n\n";
open (pass, "$AUTHFILE") || die "can't open passwd file\n";


read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

 @pairs = split(/&/, $buffer);
 foreach $pair (@pairs)
 {
     ($name, $value) = split(/=/, $pair);
     $value =~ tr/+/ /;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     $fromweb{$name} = $value;

 }

while (<pass>)
{
 ($login, $passwd) = split(/=/, $_);
 $passwd =~ s/\W//;
 $fromfile{$login}=$passwd;
}
close (pass);

if ($fromweb{"pass"} ne $fromfile{$fromweb{"logi"}})
{  print '<html><head><title>Invalid PIN!</title>';
   print '<body bgcolor="#000000" text="#ffffff">';
   print '<center><strong>Your PIN is invalid, please use your browsers
back button to re-submit it</strong></center>';
   print  "</body></html>";
}

elseif ($name eq "version" && $value = 1)
{  print '<html><head><title>Verifying PIN...</title><META
HTTP-EQUIV="Refresh"
CONTENT="2;URL=http://www.fit2print.com/member/member_low_graphics.html"></head>';

   print "<body bgcolor=#000000 text=#ffffff>";
   print '<strong>Verifying PIN.......</strong>';
   print "</body></html>";
}
else (order_page;}

sub order_page {
   print '<html><head><title>Verifying PIN...</title><META
HTTP-EQUIV="Refresh"
CONTENT="2;URL=http://www.fit2print.com/member/member_index.html"></head>';

   print "<body bgcolor=#000000 text=#ffffff>";
   print '<strong>Verifying PIN.......</strong>';
   print "</body></html>";

}





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

Date: 21 Sep 1998 01:17:59 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Danish letters =?iso-8859-1?Q?=C6_=D8_=C5?=
Message-Id: <slrn70baa7.3u7.sholden@pgrad.cs.usyd.edu.au>

[A copy of this news post was Cc:d to tcd@post10.tele.dk]

On Sun, 20 Sep 1998 19:17:12 +0200, tcd@post10.tele.dk <tcd@post10.tele.dk>
	wrote:
>Thanx for nothing

Well you just ruined any chance you had of getting help from virtually everyone
who might be able to help you on cplm. Congratulations you I lasted for almost
3 weeks this month without this but : my first *plonk* of Sept...

locales are _exactly_ what you want unless your system doesn't support them, in
which case I'm sure someone could have helped you... but I can't see that 
happening now.


>
>Jarkko Hietaniemi wrote:
>> 
>> tcd@post10.tele.dk writes:
>> 
>> > I have been looking at some database scripts
>> > And it seems that they all have the same fault.
>> > When you do a search for a word that starts with a danish letter
>> > F X E f x e - like in the words  "Fg" "Xlstykke" "Erhus"
>> > the scripts cant find it.
>> >
>> >
>> > I'm not sure but this seems to be a problem with
>> > perls reg expression matching, not the programs..
>> 
>> Wrongo.  Probably.  If the regexps are using \w, that can be remedied,
>> at least if the underlying operating system support the notion of 'locales'.
>> That means: mostly just the UNIX platforms.
>> 
>> Short summary: (1) setup your locale (2) Add "use locale;" to your
>> Perl programs.
>> 
>> Long story: perllocale.  You really want to read it because (1) cannot
>> really be done without knowing the details.
>> 
>> > Can anyone help me with this problem ????????????
>> > Please email me if you have the answer to the problem.
>> 
>> It's a bit rude to ask for free private consulting.  Other people
>> may be struggling with the same problem.  The news system is about
>> sharing, it's not a helpdesk.


-- 
Sam

Simple rule: include files should never include include files.
	--Rob Pike


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

Date: 21 Sep 1998 01:45:28 GMT
From: maeda@src.ricoh.co.jp
Subject: Does perl5 have a switch statement?
Message-Id: <MIDORIN.98Sep21104528@arima.cc.rim.or.jp>

Perl4 produces a "jump table" for a "switch-like" construct as:
	while (<>) {
	    $a=0, next if $_ == 10;
	    $a=1, next if $_ == 9;
	    $a=2, next if $_ == 8;
	    $a=3, next if $_ == 7;
	    $a=4, next if $_ == 6;
	    $a=5, next if $_ == 5;
	    $a=6, next if $_ == 4;
	    $a=7;
	} continue {
	    print $a, $_;
	}
Actually, perl4.036 -D1024 gives a node whose name is NSWITCH:
    CC_TRUE = {
        C_TYPE = NSWITCH
        C_ADDR = 0x6fac8
        C_NEXT = 0x76d08
        C_OPT = CFT_FALSE
        C_FLAGS = ()
        C_STAB = {
            STAB_NAME = main'_            
        }
        C_EXPR = NULL
        SC_MIN = (4)
        SC_MAX = (10)
        SC_NEXT[LT] = 0x76b08
        SC_NEXT[4] = 0x769c8
        SC_NEXT[5] = 0x76748
        SC_NEXT[6] = 0x764c8
        SC_NEXT[7] = 0x76248
        SC_NEXT[8] = 0x75fc8
        SC_NEXT[9] = 0x75d48
        SC_NEXT[10] = 0x76d08
        SC_NEXT[GT] = 0x76b08
    }


If I use m// instead of $_ ==, I still get a similar node now
with CSWITCH.

I believe this was the answer to an FAQ: "Why doesn't perl have
a switch statement?" and the answer used to be "It actually
does.  Look at -D1024 output."

Today I found all these don't apply to perl5.005.  Doesn't perl5
produce "jump tables" any longer?  Where can I find
documentations or Changes5.00X for the reason perl5 not making
jump tables?  Or am I missing something?

P.S.
I posted this a few days ago but see no follow-ups yet.
When you answer, please send me a mail at the same time.
Sorry for duplication.
                                                       _  __  _
-------------------------------  MadCat        LRM15  [ ]/  \[ ]  LRM15
 Kaoru "Mad Player" MAEDA        75t 175km/h     LG+  o@ \__/ @o  AFC100
 maeda@src.ricoh.co.jp           HeatSink 28     Sm+   .=X  X=.   Sm+
-------------------------------  Armor 2195           _|_    _|_


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

Date: Sun, 20 Sep 1998 22:13:32 -0400
From: Bill 'Sneex' Jones <sneaker@sneex.fccj.org>
Subject: Re: Does perl5 have a switch statement?
Message-Id: <3605B64C.C55702FE@sneex.fccj.org>

maeda@src.ricoh.co.jp wrote:
> 
<snipped weird stuff...>

Try something like:

     last if (substr($vFlag, 1, 3) eq 'END');
$vSub = \&Sneex   if (substr($vFlag, 1, 5) eq 'SNEEX');
$vSub = \&Admin   if (substr($vFlag, 1, 5) eq 'ADMIN');
$vSub = \&Reports if (substr($vFlag, 1, 7) eq 'REPORTS');
$vSub = \&Logs    if (substr($vFlag, 1, 4) eq 'LOGS');
$vSub = \&Targets if (substr($vFlag, 1, 7) eq 'TARGETS');
$vSub = \&Usenet  if (substr($vFlag, 1, 6) eq 'USENET');

(substr($_, 0, 1) eq '[') ? next    # On [TAG], read next line.
			  : &$vSub; # Else, call subroutine...

Obviously not complete code, but something this group's
'cave dwellers' helped me on when I was searching for
a 'switch' statement as well.

May give you some ideas or a headache,

Enjoy!
-Sneex-  :]
__________________________________________________________________
Bill Jones | FCCJ Webmaster | http://webmaster.fccj.org/Webmaster
__________________________________________________________________
We are the CLPM... Lower your standards and surrender your code...
We will add your biological and technological distinctiveness to 
our own... Your thoughts will adapt to service us...
 ...Resistance is futile...


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

Date: 21 Sep 1998 02:40:03 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: Does perl5 have a switch statement?
Message-Id: <6u4ea3$tsn$1@netnews.upenn.edu>

In article <MIDORIN.98Sep21104528@arima.cc.rim.or.jp>,
 <maeda@src.ricoh.co.jp> wrote:
>Today I found all these don't apply to perl5.005.  Doesn't perl5
>produce "jump tables" any longer?  Where can I find
>documentations or Changes5.00X for the reason perl5 not making
>jump tables?  Or am I missing something?

You are correct; the optimization to jump tables disappeared between
Perl 4 and Perl 5.  I do not know why.  There was some discussion on
p5p last December about putting it back in, but it got muddled up
because nobody could decide what it should do.

If you submit a patch, it might be accepted.


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

Date: Sun, 20 Sep 1998 21:17:25 -0500
From: phenix@interpath.com (John Moreno)
Subject: Re: even/odd numbers
Message-Id: <1dfo978.z9u47r1kmwglfN@roxboro0-058.dyn.interpath.net>

Josh Kortbein <kortbein@iastate.edu> wrote:

> John Moreno (phenix@interpath.com) wrote:
> : > Well if people keep asking it day in and day out it probably ought to
> : > be in the FAQ, which of course is where I first looked. Just keep in
> : > mind not all of us here are experienced programmers and the logic you
> : > express in your solutions isn't the same as one finds in a high school
> : > algebra class.
> 
> : As someone else pointed out - this isn't a perl question, it is a basic
> : mathematics question.
> 
> As a mathematician-in-training, I'd like to point out that "modulus" often
> isn't introduced for quote some time, depending on when one takes which
> classes. If one takes algebra, geometry, trigonometry, calculus, etc.,
> without ever taking a "discrete math" course, it could be that division
> modulo n might not even be introduced until a first course in abstract
> algebra. Yes, the way it works is basic, but the idea of something like
> the modulus operator is largely useless in lots of mathematical
> situations.

"modulus" is of course the easiest and most straightforward way of
finding out, but it's hardly the only (although the most obvious way
would much simplier if perl had a div command like pascal).

-- 
John Moreno


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

Date: 21 Sep 1998 01:34:23 GMT
From: kortbein@iastate.edu (Josh Kortbein)
Subject: Re: even/odd numbers
Message-Id: <6u4aev$b76$4@news.iastate.edu>

John Moreno (phenix@interpath.com) wrote:
: Josh Kortbein <kortbein@iastate.edu> wrote:

: > John Moreno (phenix@interpath.com) wrote:
: > : > Well if people keep asking it day in and day out it probably ought to
: > : > be in the FAQ, which of course is where I first looked. Just keep in
: > : > mind not all of us here are experienced programmers and the logic you
: > : > express in your solutions isn't the same as one finds in a high school
: > : > algebra class.
: > 
: > : As someone else pointed out - this isn't a perl question, it is a basic
: > : mathematics question.
: > 
: > As a mathematician-in-training, I'd like to point out that "modulus" often
: > isn't introduced for quote some time, depending on when one takes which
: > classes. If one takes algebra, geometry, trigonometry, calculus, etc.,
: > without ever taking a "discrete math" course, it could be that division
: > modulo n might not even be introduced until a first course in abstract
: > algebra. Yes, the way it works is basic, but the idea of something like
: > the modulus operator is largely useless in lots of mathematical
: > situations.

: "modulus" is of course the easiest and most straightforward way of
: finding out, but it's hardly the only (although the most obvious way
: would much simplier if perl had a div command like pascal).

The original poster knew at least one other way of determining parity.
He wanted some sort of builtin function for doing so, i.e. modulus.
Which is not straightfoward to find out if you don't know what a modulus
is.


Josh

-- 
        The mechanics of writing can be taught, the basic
        rules and tools and guidelines, but as in any art,
        all the rest comes from doing and doing and doing.
        The most important part of the process is learning
        to trust one's own vision, to work hard at developing
        one's own style and voice, and then having the
        confidence to follow where it goes.

                        - Mary McGarry Morris



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

Date: Mon, 21 Sep 1998 02:41:27 GMT
From: Greg Hersh <ghersh@bbnplanet.com>
Subject: getting the pid of the process started with 'open'
Message-Id: <3605BCC5.184@bbnplanet.com>

On Solaris, I issue open to start some process. The pid returned
is the 'open process' pid, say 1111. This process in turn starts
the one I've requested, with pid 1112 (or whatever). Is there a way
a caller get that last pid (1112)? (Runing perl5.004_04. A Camel book
as well as FAQ explains the sequence of the events but still doesn't
answer this question). 

Thanks,



-- 
Greg


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

Date: 21 Sep 1998 05:09:34 +0300
From: jari.aalto@poboxes.com (Jari Aalto+mail.perl)
Subject: How to combine hash %b to hash %a? (union)
Message-Id: <ptr90jemd29.fsf@olkikukka.i-have-a-misconfigured-system-so-shoot-me>


	I feel little stupid because I can't figure out the

		%a{%b}

	syntax to add hash %b into existing hash %a

		%a = ( aa => 11, bb => 22 );
		%b = ( a => 1, b => 2 );

	I found only one match for "union" from perl 04.04 faq4, but
	that dealt with arrays. I know I can write map or for loop for
	this, but I was wondering if I can do it with variable
	assignment method somehow.

	I assume this is faq material....

	jari
	


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

Date: Mon, 21 Sep 1998 02:36:45 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: How to combine hash %b to hash %a? (union)
Message-Id: <1ZiN1.1343$BV5.205611@nsw.nnrp.telstra.net>

In article <ptr90jemd29.fsf@olkikukka.i-have-a-misconfigured-system-so-shoot-me>,
	jari.aalto@poboxes.com (Jari Aalto+mail.perl) writes:

> 	syntax to add hash %b into existing hash %a

One possibility:

%a = (%a, %b);

Martien
-- 
Martien Verbruggen                  | 
Webmaster www.tradingpost.com.au    | Advertising:  The science of arresting
Commercial Dynamics Pty. Ltd.       | the human intelligence long enough to
NSW, Australia                      | get money from it.


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

Date: Mon, 21 Sep 1998 02:41:49 GMT
From: Rick Delaney <rick.delaney@shaw.wave.ca>
Subject: Re: How to combine hash %b to hash %a? (union)
Message-Id: <3605BE7E.2E8864E9@shaw.wave.ca>

[posted & mailed]

Jari Aalto+mail.perl wrote:
> 
>         I feel little stupid because I can't figure out the
> 
>                 %a{%b}
> 
>         syntax to add hash %b into existing hash %a

This syntax wouldn't make much sense, since you would be dropping keys
and values of %b into where only the keys of %a should go.  Plus a hash
slice must start with an @.

    @a{keys %b} = values %b;# this'll do

-- 
Rick Delaney
rick.delaney@shaw.wave.ca


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

Date: Sun, 20 Sep 1998 22:57:36 -0400
From: "Matthew O. Persico" <mpersico@erols.com>
Subject: Re: How to combine hash %b to hash %a? (union)
Message-Id: <3605C0A0.28681A75@erols.com>

Because everyone should remember that a hash USED to be called an
associative array. Therefore, once you get over the % and the =>, it is
an array and can be treated as such under many circumstances.

Martien Verbruggen wrote:
> 
> In article <ptr90jemd29.fsf@olkikukka.i-have-a-misconfigured-system-so-shoot-me>,
>         jari.aalto@poboxes.com (Jari Aalto+mail.perl) writes:
> 
> >       syntax to add hash %b into existing hash %a
> 
> One possibility:
> 
> %a = (%a, %b);
> 
> Martien
> --
> Martien Verbruggen                  |
> Webmaster www.tradingpost.com.au    | Advertising:  The science of arresting
> Commercial Dynamics Pty. Ltd.       | the human intelligence long enough to
> NSW, Australia                      | get money from it.

-- 
Get "The Perl Cookbook"
Christansen and Torkington. 
O'Reilly. ISBN 1-56592-243-3
FAQ on serious steriods!


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

Date: Mon, 21 Sep 1998 01:51:25 GMT
From: "Garry T. Williams" <garry@america.net>
Subject: Re: Number Sort
Message-Id: <3605B14F.383E69EB@america.net>

Larry Rosler wrote:
> 
> [Posted to comp.lang.perl.misc and copy mailed.]
> 
> In article <36057963.9A2CC13F@america.net> on Sun, 20 Sep 1998 21:52:27
> GMT, Garry T. Williams <garry@america.net> says...
> ...
> > sub numerically {
> >     ($akey) = split(/\|/, $a);
> >     ($bkey) = split(/\|/, $b);
> >     $akey <=> $bkey;
> > };
> >
> > @anarray = sort numerically @anarray;
> 
> Slow, slow, slow!  You want to minimize the amount of computation done in
> the sort subroutine, which is executed O(n log n) times.
> 
> For the more efficient approach, read the FAQ and the FMTYEWTK about
> sorting to which it points.  Once you get the hang of the Schwartzian
> Transform, you can pour it out by rote:
> 
> @anarray = map $_->[0] => sort { $a->[1] <=> $b->[1] }
>     map { [ $_, (split /\|/)[0] ] } @anarray;

Yes, you're right.  I read the FAQ on this long ago and only skimmed it
at that.  The sort operator calls the comparison block *each* time it
needs a compare of two records (list elements).  Although the number is
not n log n, it's even bigger!  (_The Perl Cookbook_ states that for
1,000 elements, there will be 14,000 comparisons -- which is about 2n ln
n.  That means 28,000 split operations for 1,000 items in the original
list!)  My solution is far too slow as the number of elements in the
list to be sorted increases.  

The secret to the "Swartzian transform" is to compute the data to be
compared before letting the sort have it so that the computation only
needs to be done once for each element of the list.  The second map is
handing sort a list of references to anonymous arrays.  Each anonymous
array contains the original record and the number we want to compare to
order the original list.  That, in turn minimizes the computation in the
actual comparison block.  It's really not mysterious, once you break it
down into its component parts.  

Thanks, Larry.  I guess I'll read the FAQ again.  :-)  

-Garry Williams


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

Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>


Administrivia:

Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.

If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu. 


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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 3769
**************************************

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