[13772] in Perl-Users-Digest
Perl-Users Digest, Issue: 1182 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 27 21:57:28 1999
Date: Wed, 27 Oct 1999 18:57:13 -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: <941075833-v9-i1182@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 27 Oct 1999 Volume: 9 Number: 1182
Today's topics:
Re: helponlambdacalculus c_j_marshall@my-deja.com
Re: helponlambdacalculus (Abigail)
Re: Hex to binary? MTEYEWTK... <sb@sdm.de>
Hex to binary? <patrick45@hotmail.com>
Re: Hex to binary? <skilchen@swissonline.ch>
Re: Hex to binary? <lr@hpl.hp.com>
Re: Hex to binary? <dchristensen@california.com>
Re: Hex to binary? <theglauber@my-deja.com>
Re: Hex to binary? <dchristensen@california.com>
Re: Hex to binary? <lr@hpl.hp.com>
Hide private files <me@toao.net>
Hide private files <me@toao.net>
Re: Hide private files <rootbeer@redcat.com>
How can I control the enviroment? <henryd@net-gong.com>
Re: How can I control the enviroment? <gellyfish@gellyfish.com>
How can I improve BLOB data operation speed? <cosmos@hanmesoft.co.kr>
Re: How can I improve BLOB data operation speed? (Michael Budash)
Re: How can I join two hashes? <rhomberg@ife.ee.ethz.ch>
Re: How can I join two hashes? <rhomberg@ife.ee.ethz.ch>
Re: How can I join two hashes? <rick.delaney@home.com>
Re: How can I join two hashes? <rick.delaney@home.com>
Re: How can I join two hashes? <lr@hpl.hp.com>
Re: How can I join two hashes? <aqumsieh@matrox.com>
Re: How can I join two hashes? <rhomberg@ife.ee.ethz.ch>
Re: How can I join two hashes? <rhomberg@ife.ee.ethz.ch>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 27 Oct 1999 14:26:47 GMT
From: c_j_marshall@my-deja.com
Subject: Re: helponlambdacalculus
Message-Id: <7v7232$vff$1@nnrp1.deja.com>
>
> That is correct.
>
> But what does that have to do with the topic of this newsgroup?
>
> The sole reason of this group is to flame newbies for being newbies.
>
I thought its purpose was to wind up the old-hands by allowing people tp
continually ask CGI questions and avoiding at all costs looking in the
faq for trivial questions ?
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 27 Oct 1999 07:32:53 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: helponlambdacalculus
Message-Id: <slrn81ds6p.bjj.abigail@alexandra.delanet.com>
Lorenzo Monteforte (loreglor@tin.it) wrote on MMCCXXXVI September
MCMXCIII in <URL:news:7uc52f$qja$1@nslave1.tin.it>:
"" I' m a beginner in lambda-calculus. What I understand of Lambda-calculus I
"" can manage objects that are themselves functions.
That is correct.
But what does that have to do with the topic of this newsgroup?
The sole reason of this group is to flame newbies for being newbies.
Abigail
--
package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
print } sub __PACKAGE__ { &
print ( __PACKAGE__)} &
__PACKAGE__
( )
-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
------------------------------
Date: 26 Oct 1999 12:54:38 GMT
From: Steffen Beyer <sb@sdm.de>
Subject: Re: Hex to binary? MTEYEWTK...
Message-Id: <7v48ae$k3k$5@solti3.sdm.de>
-------------------- cut here -------------------- cut here --------------------
Suggestion for the perlfaq4 manpage:
------------------------------------
How do I convert from hexadecimal to decimal:
1) $dec = 0xDEADBEEF;
2) $dec = hex("DEADBEEF");
3) $dec = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8)));
4) use Bit::Vector;
$vec = Bit::Vector->new_Hex(32, "DEADBEEF");
$dec = $vec->to_Dec();
How do I convert from decimal to hexadecimal:
1) $hex = sprintf("%X", 3735928559);
2) $hex = unpack("H*", pack("N", 3735928559));
3) use Bit::Vector;
$vec = Bit::Vector->new_Dec(32, -559038737);
$hex = $vec->to_Hex();
How do I convert from octal to decimal:
1) $dec = 033653337357; # note the leading '0'
2) $dec = oct("33653337357");
3) use Bit::Vector;
$vec = Bit::Vector->new(32);
$vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
$dec = $vec->to_Dec();
How do I convert from decimal to octal:
1) $oct = sprintf("%o", 3735928559);
2) use Bit::Vector;
$vec = Bit::Vector->new_Dec(32, -559038737);
$oct = reverse join('', $vec->Chunk_List_Read(3));
How do I convert from binary to decimal:
1) $dec = unpack("N", pack("B32",
substr("0" x 32 . "11011110101011011011111011101111", -32)));
2) use Bit::Vector;
$vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
$dec = $vec->to_Dec();
How do I convert from decimal to binary:
1) $bin = unpack("B*", pack("N", 3735928559));
2) use Bit::Vector;
$vec = Bit::Vector->new_Dec(32, -559038737);
$bin = $vec->to_Bin();
The remaining transformations (e.g. hex -> oct, bin -> hex, etc.)
are left as an exercise to the inclined reader. :-)
Note: The advantage of the Bit::Vector module is that it works with
numbers of ANY size and that it's designed for speed (it's written
in C, internally).
-------------------- cut here -------------------- cut here --------------------
hex -> bin:
-----------
1) use Bit::Vector;
$vec = Bit::Vector->new_Hex($bits, $hex);
$bin = $vec->to_Bin();
See the URL in my sig below where to download this module from.
HTH.
Best regards,
--
Steffen Beyer <sb@engelschall.com>
http://www.engelschall.com/u/sb/whoami/ (Who am I)
http://www.engelschall.com/u/sb/gallery/ (Fotos Brasil, USA, ...)
http://www.engelschall.com/u/sb/download/ (Free Perl and C Software)
------------------------------
Date: Mon, 25 Oct 1999 20:45:48 +0200
From: Patrick <patrick45@hotmail.com>
Subject: Hex to binary?
Message-Id: <3814A55B.B9F49C19@hotmail.com>
Hi there,
I am relatively new to Perl and have been struggling for a while
now with the problem
of how to convert a Hexadicimal octet I have stored in a variable to a
binary string.
I found the following way if you explicity load the hex value (h'ff for
example) into the variable:
$value="\xff";
$binary_string = join('', unpack('B*', $value));
The problem is I assign the value in $value in a pattern search so it
can have various values.
I then noticed if $value=9F say, that the binary value being created was
that interpreting
9F as a character string.
I tried then to make sure that $value would contain a hex value by:
$hex_value=unpack("h2",$value);
$binary_string = join('', unpack('B*', $hex_value));
Still no luck though.... The only other hexadecimal references I can
find in the
documentation concern the hex function which would just return the
decimal
value of my string 9F read as hex...but can I specify in some way that
the
string contained in $value is a hexadecimal and not a character one??
Many thanks for any pointers!
Regards /
Patrick.
------------------------------
Date: Tue, 26 Oct 1999 06:01:18 GMT
From: "Samuel Kilchenmann" <skilchen@swissonline.ch>
Subject: Re: Hex to binary?
Message-Id: <OsbR3.26582$m4.96422945@news.magma.ca>
Patrick <patrick45@hotmail.com> wrote in message
news:3814A55B.B9F49C19@hotmail.com...
>
>
> $hex_value=unpack("h2",$value);
what about:
$hex_value=pack("h2",$value);
or
$hex_value=pack("H2",$value);
> $binary_string = join('', unpack('B*', $hex_value));
>
On my systems (little endians, i am just too lazy to check if byte
order does matter here) the following:
my $hexstr = "9F";
print " hex: ", $hexstr, "\n";
my $bstr = (unpack("B*", pack("H*", $hexstr)))[0];
print " binary: ", $bstr, "\n";
$bstr = substr("0" x 32 . $bstr, -32);
my $number = (unpack("N", pack("B32", $bstr)))[0];
print "decimal: ", $number, "\n";
prints:
hex: 9F
binary: 10011111
decimal: 159
as expected
------------------------------
Date: Mon, 25 Oct 1999 16:47:53 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Hex to binary?
Message-Id: <MPG.127e8c05a189333e98a126@nntp.hpl.hp.com>
In article <7v2j5g$psr$1@nnrp1.deja.com> on Mon, 25 Oct 1999 21:47:30
GMT, The Glauber <theglauber@my-deja.com> says...
> In article <3814A55B.B9F49C19@hotmail.com>,
> Patrick <patrick45@hotmail.com> wrote:
...
> > ... how to convert a Hexadicimal octet I have stored in a variable to a
> > binary string.
...
> i'm no guru, and i'm sure there is a better way, but this may help:
Not likely, because the input is an eight-character string
representation of an integer, and the output is not a binary string.
> $hstr = "5065726C";
> $result = "";
> $hstrlen = length($hstr);
> for ( $pos = 0; $pos < $hstrlen; $pos += 2 )
> {
> $result .= sprintf( "%c", hex(substr($hstr, $pos, 2)) );
> }
> print $result, "\n"
>
> This should print the word "Perl".
So should this:
print unpack('A*' => pack 'H*' => $hstr), "\n";
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 25 Oct 1999 16:02:48 -0700
From: "David Christensen" <dchristensen@california.com>
Subject: Re: Hex to binary?
Message-Id: <7v2mmj$bhg$1@pollux.dnai.com>
Patrick:
> I am relatively new to Perl and have been struggling for a while
> now with the problem of how to convert a Hexadicimal octet I have
> stored in a variable to a binary string.
I tried pack() and unpack(), and couldn't figure them out either.
So, here is the C programmer's answer:
1 #! perl -w
2 use strict;
3
4 my @chars = split //, $ARGV[0];
5 my $binary = "";
6 my %lookup = (
7 '0'=>'0000', '1' =>'0001', '2'=>'0010', '3'=>'0011',
8 '4'=>'0100', '5' =>'0101', '6'=>'0110', '7'=>'0111',
9 '8'=>'1000', '9' =>'1001', 'A'=>'1010', 'B'=>'1011',
10 'C'=>'1100', 'D' =>'1101', 'E'=>'1110', 'F'=>'1111',
11 );
12
13 foreach (@chars) {
14 $_ = uc();
15 die "$_ is not a hex digit" unless /[\dA-F]/;
16 $binary .= $lookup{$_};
17 }
18
19 print "$binary\n";
--
David Christensen
dchristensen@california.com
------------------------------
Date: Mon, 25 Oct 1999 21:47:30 GMT
From: The Glauber <theglauber@my-deja.com>
To: Patrick <patrick45@hotmail.com>
Subject: Re: Hex to binary?
Message-Id: <7v2j5g$psr$1@nnrp1.deja.com>
In article <3814A55B.B9F49C19@hotmail.com>,
Patrick <patrick45@hotmail.com> wrote:
>
> Hi there,
> I am relatively new to Perl and have been struggling for a
while
> now with the problem
> of how to convert a Hexadicimal octet I have stored in a variable to a
> binary string.
[...]
Hello, Patrick,
i'm no guru, and i'm sure there is a better way, but this may help:
$hstr = "5065726C";
$result = "";
$hstrlen = length($hstr);
for ( $pos = 0; $pos < $hstrlen; $pos += 2 )
{
$result .= sprintf( "%c", hex(substr($hstr, $pos, 2)) );
}
print $result, "\n"
This should print the word "Perl".
Glauber
--
Glauber Ribeiro
theglauber@my-deja.com
"Opinions stated are my own and not representative of Experian"
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 25 Oct 1999 16:06:47 -0700
From: "David Christensen" <dchristensen@california.com>
Subject: Re: Hex to binary?
Message-Id: <7v2mto$bi4$1@pollux.dnai.com>
Patrick:
> I am relatively new to Perl and have been struggling for a while
> now with the problem of how to convert a Hexadicimal octet I have
> stored in a variable to a binary string.
I tried pack() and unpack(), and couldn't figure them out either.
So, here is the C programmer's answer:
1 #! perl -w
2 use strict;
3
4 my @chars = split //, $ARGV[0];
5 my $binary = "";
6 my %lookup = (
7 '0'=>'0000', '1' =>'0001', '2'=>'0010', '3'=>'0011',
8 '4'=>'0100', '5' =>'0101', '6'=>'0110', '7'=>'0111',
9 '8'=>'1000', '9' =>'1001', 'A'=>'1010', 'B'=>'1011',
10 'C'=>'1100', 'D' =>'1101', 'E'=>'1110', 'F'=>'1111',
11 );
12
13 foreach (@chars) {
14 $_ = uc();
15 die "$_ is not a hex digit" unless /[\dA-F]/;
16 $binary .= $lookup{$_};
17 }
18
19 print "$binary\n";
Sample runs:
~/code/perl/hex2binary$ ./hex2binary 10
00010000
~/code/perl/hex2binary$ ./hex2binary f0f0
1111000011110000
--
David Christensen
dchristensen@california.com
------------------------------
Date: Mon, 25 Oct 1999 13:25:02 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Hex to binary?
Message-Id: <MPG.127e5c778344cc3798a122@nntp.hpl.hp.com>
In article <3814A55B.B9F49C19@hotmail.com> on Mon, 25 Oct 1999 20:45:48
+0200, Patrick <patrick45@hotmail.com> says...
> I am relatively new to Perl and have been struggling for a while
> now with the problem
> of how to convert a Hexadicimal octet I have stored in a variable to a
> binary string.
I am struggling with the problem of reading your post despite the
bizarre line wrapping produced by your 'newsreader' -- X-Mailer: Mozilla
4.06 [en] (X11; I; SunOS 5.6 sun4m). Please set it simply to line-wrap
at 72 characters.
...
> ... but can I specify in some way that
> the
> string contained in $value is a hexadecimal and not a character one??
A string is a sequence of characters. I think you are trying to say
that you want to convert an integer value (that you have chosen to
represent as a sequence of 8 hexadecimal characters -- what you call a
'Hexadicimal octet') into a string of 1s and 0s that represents the same
integer in binary.
#!perl -w
use strict;
my $value = 0xDEADBEEF; # An integer, not a string!
my $binary_string = unpack 'B*' => pack 'N' => $value;
print $binary_string, "\n";
# Or, more legibly, as octets:
print map("$_ " => $binary_string =~ /.{8}/g), "\n";
__END__
Note that in Perl 5.006 this will be handled by a simple [s]printf '%b'
or '%B' format specifier.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 26 Oct 1999 21:47:03 GMT
From: Graham W. Boyes <me@toao.net>
Subject: Hide private files
Message-Id: <7v57gm$muc$1@nnrp1.deja.com>
Is there a way to make a file & directory "hidden" on the WWW? I am
storing a files that contain passwords for my website, but I would like
these files only to be accessable if I call them within a Perl script
(to check to see if the password the user entered is correct), not if I
just type the address in a browser.
If not, what is the best way to use a password system with Perl?
I have tried CHMOD'ing the files, but it seems to have no affect this
way.
--
"The One and Only"
me AT toao DOT net ~ http://www.toao.net
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Tue, 26 Oct 1999 21:47:10 GMT
From: Graham W. Boyes <me@toao.net>
Subject: Hide private files
Message-Id: <7v57gt$mue$1@nnrp1.deja.com>
Is there a way to make a file & directory "hidden" on the WWW? I am
storing a files that contain passwords for my website, but I would like
these files only to be accessable if I call them within a Perl script
(to check to see if the password the user entered is correct), not if I
just type the address in a browser.
If not, what is the best way to use a password system with Perl?
I have tried CHMOD'ing the files, but it seems to have no affect this
way.
Thanks,
Graham W. Boyes
--
"The One and Only"
me AT toao DOT net ~ http://www.toao.net
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Tue, 26 Oct 1999 15:56:37 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Hide private files
Message-Id: <Pine.GSO.4.10.9910261553470.29843-100000@user2.teleport.com>
On Tue, 26 Oct 1999, Graham W. Boyes wrote:
> Is there a way to make a file & directory "hidden" on the WWW?
It sounds as if you want to configure your webserver to not serve a
certain file. You should probably check with your local expert or
webmaster. Even if we knew the answer for your site (which we don't), we
shouldn't discuss it in a newsgroup which is about Perl.
Good luck!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Wed, 27 Oct 1999 14:50:54 +0200
From: "David Henry" <henryd@net-gong.com>
Subject: How can I control the enviroment?
Message-Id: <7v6smv$qo$1@news2.inter.net.il>
The Informix DBD driver needs to read the environment variable
INFORMIXSERVER to get the name of the server to access. In my case this must
be set on the fly as there are a number of servers that I can access.
How can I alter the environment from Perl so that the DBD driver also sees
the change? I tried adding to the %ENV hash but it doesn't help, looks like
%ENV is just a local copy.
I've gone through 'Perl in a nutshell' but cannot find anything that seems
suitable.
In a nutshell, I'm looking for something like the C function, setenv.
------------------------------
Date: 27 Oct 1999 16:17:29 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: How can I control the enviroment?
Message-Id: <38171789_1@newsread3.dircon.co.uk>
David Henry <henryd@net-gong.com> wrote:
> The Informix DBD driver needs to read the environment variable
> INFORMIXSERVER to get the name of the server to access. In my case this must
> be set on the fly as there are a number of servers that I can access.
> How can I alter the environment from Perl so that the DBD driver also sees
> the change? I tried adding to the %ENV hash but it doesn't help, looks like
> %ENV is just a local copy.
You will need to set $ENV{INFORMIXSERVER} then disconnect and connect again
to use the new server ...
/J\
--
"The most frightening thing on television since Anthea Turner revealed
she had a sister" - Suggs
------------------------------
Date: Mon, 25 Oct 1999 19:04:20 +0900
From: "Kim, Jae-In" <cosmos@hanmesoft.co.kr>
Subject: How can I improve BLOB data operation speed?
Message-Id: <MI5DgDtH$GA.223@news.thrunet.com>
I'm using ms-sql, and connect to ODBC, DBI, very large object(maybe 500K
above) insert to ms-sql.
and retrive it in web pages.
but, the response time is incredible (wait 2~3 minute)
my impliment is:
$dbh=DBI->connect('dbi:ODBC:HMWEncy','sa','',{ RaiseError=> 1});
$dbh->{LongReadLen}= 500000;
$qryStmt = "SELECT * FROM WebTexts where TitleId = " . $id;
$cursor = $dbh->prepare($qryStmt);
$cursor->execute;
print "<HTML>\n<HEAD>\n";
print "<TITLLE>Search result</TITLE>\n";
print "<BODY>\n";
while (($id, $body) = $cursor->fetchrow_array) {
print $body;
}
print "</TD><TR>\n";
print "</BODY>\n</HTML>";
my question is, How can I do for improve speed?
thanks.
----
Kim, Jae-In
cosmos@hanmesoft.co.kr
------------------------------
Date: Mon, 25 Oct 1999 03:36:53 -0700
From: mbudash@sonic.net (Michael Budash)
Subject: Re: How can I improve BLOB data operation speed?
Message-Id: <mbudash-2510990336530001@adsl-216-103-91-123.dsl.snfc21.pacbell.net>
In article <MI5DgDtH$GA.223@news.thrunet.com>, "Kim, Jae-In"
<cosmos@hanmesoft.co.kr> wrote:
>I'm using ms-sql, and connect to ODBC, DBI, very large object(maybe 500K
>above) insert to ms-sql.
>and retrive it in web pages.
>
>but, the response time is incredible (wait 2~3 minute)
>
>my impliment is:
>
>$dbh=DBI->connect('dbi:ODBC:HMWEncy','sa','',{ RaiseError=> 1});
>$dbh->{LongReadLen}= 500000;
>$qryStmt = "SELECT * FROM WebTexts where TitleId = " . $id;
>$cursor = $dbh->prepare($qryStmt);
>$cursor->execute;
>
>print "<HTML>\n<HEAD>\n";
>print "<TITLLE>Search result</TITLE>\n";
>print "<BODY>\n";
>
>while (($id, $body) = $cursor->fetchrow_array) {
> print $body;
>}
>
>print "</TD><TR>\n";
>print "</BODY>\n</HTML>";
>
>my question is, How can I do for improve speed?
>
this is not a perl question and should have been posted to comp.databases,
but since you're here...
is there an index on TitleId? if not, build one and retry...
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: Tue, 26 Oct 1999 17:14:08 +0200
From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
Subject: Re: How can I join two hashes?
Message-Id: <3815C540.B0F7374D@ife.ee.ethz.ch>
Rick Delaney wrote:
>
> Larry Rosler wrote:
> >
> > Sorry, that is just not true. The Benchmark module makes no distinction
> > between lexical variables and global variables. Here are my results for
> > the code above, changed only to declare hashes instead of scalars (so it
> > now runs with 'use strict;'):
>
> It does actually make a difference depending on whether you pass strings
> for eval'ing or coderefs. I think that may be what has confused Alex
> since he has been cutting and pasting my benchmarks elsewhere in this
> thread.
Yes, that's it. I was mainly confused because I avoid coderefs. Calling
the subs often takes more time than running the benchmarked parts.
- Alex
------------------------------
Date: Tue, 26 Oct 1999 10:50:02 +0200
From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
To: Larry Rosler <lr@hpl.hp.com>
Subject: Re: How can I join two hashes?
Message-Id: <38156B3A.81984967@ife.ee.ethz.ch>
Larry Rosler wrote:
>
> In article <3814432C.D55D507B@ife.ee.ethz.ch> on Mon, 25 Oct 1999
> 13:46:52 +0200, Alex Rhomberg <rhomberg@ife.ee.ethz.ch> says...
> + Something does change
> + The funny thing is, the tests _only_ work because he my'ed the wrong
> + variables.
> + Benchmark uses the globals, not lexical variables. I found this out,
> + when sorting *huge* arrays was incredibly fast :-)
> + if he changes the my, his benchmark breaks.
>
> Sorry, that is just not true. The Benchmark module makes no distinction
> between lexical variables and global variables.
Then I do not understand this:
--------------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use Benchmark;
use vars qw/$s/;
$s = 'hallo';
my $s = 'blops';
my $t = 'lalala';
timethese(1,{ test=> q{print "$s\n" unless defined $t}});
--------------------------------------------------------------------------
Benchmark: timing 1 iterations of test...
hallo
test: 0 secs ( 0.00 usr 0.00 sys = 0.00 cpu)
(warning: too few iterations for a reliable count)
--------------------------------------------------------------------------
This indicates to me that inside the benchmark block, $s eq 'hallo' and
$t eq undef.
Which is very strange when I look at the other tests with switching
scope that I just made.
I ran into that problem when sorting huge arrays took only some
microseconds
switching from "my" to globals changed that.
- Alex
------------------------------
Date: Tue, 26 Oct 1999 01:15:50 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: How can I join two hashes?
Message-Id: <381500D5.EAFE9EEE@home.com>
Larry Rosler wrote:
>
> In article <3814432C.D55D507B@ife.ee.ethz.ch> on Mon, 25 Oct 1999
> 13:46:52 +0200, Alex Rhomberg <rhomberg@ife.ee.ethz.ch> says...
> +
> + Benchmark uses the globals, not lexical variables. I found this out,
> + when sorting *huge* arrays was incredibly fast :-)
> + if he changes the my, his benchmark breaks.
>
> Sorry, that is just not true. The Benchmark module makes no distinction
> between lexical variables and global variables. Here are my results for
> the code above, changed only to declare hashes instead of scalars (so it
> now runs with 'use strict;'):
It does actually make a difference depending on whether you pass strings
for eval'ing or coderefs. I think that may be what has confused Alex
since he has been cutting and pasting my benchmarks elsewhere in this
thread.
If you have
my $foo = "Hello, world\n";
timethese 1, {
Coderef => sub { print "$foo\n" },
Eval => q { print "$foo\n" },
};
you will get an 'uninitialized value' warning from 'Eval' and "Hello,
world\n" from 'Coderef'.
Since the string ' print "$foo\n" ' is eval'ed in the Benchmark.pm file,
it is in an unrelated scope and can't see $foo.
Coderef works because it is a closure and holds a reference to $foo.
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Tue, 26 Oct 1999 01:01:58 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: How can I join two hashes?
Message-Id: <3814FD95.BB9E8815@home.com>
[posted & mailed]
Alex Rhomberg wrote:
>
> Rick Delaney wrote:
>
> > use Benchmark;
> > use vars qw(%h1 %h2);
> > %h1 = (a => 'b', b => 'a');
> > %h2 = (c => 'd', d => 'c');
>
> These hashes are _way_ too small for benchmarking
I think they are sufficient to make my point. You may review my
original post to see what that was.
> > timethese(100000, {
> > add1 => q { my %hash = (%h1, %h2); },
> > add2 => q { my %hash = %h1; @hash{ keys %h2 } = values %h2; },
> > add3 => q {
> > my %hash = (keys %h1, values %h1, keys %h2, values %h2)
> add3 works only for a small set of all possible hashes.
Yes, I should have removed the adding completely since that has no
relation to what I was benchmarking.
> You construct a hash of key1=>key2, key3=>key4....value1=>value2 etc.
> The funny thing is, your test hash should work. Makes me wonder if you
> wanted to confuse us...
Since it worked on you, let's just say that I did. ;-P
> now, let's look at the following benchmarks:
> =======================================================================
> #!/usr/bin/perl -w
> #use 5.005;
>
> use strict;
> use Benchmark;
> use vars qw/%hash1 %hash2 %hasha %hashb %hashc %hashd/;
>
> $hash1{$_} = rand, $hash2{"i$_"} = rand for (1..10_000);
Bigger is not necessarily better. For a proper benchmark you should use
a variety of sizes and data and compare how each method fares under the
different conditions.
I may come back later with a more complete benchmark if you don't first.
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Mon, 25 Oct 1999 10:14:23 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: How can I join two hashes?
Message-Id: <MPG.127e2fc712ceeb0a98a11c@nntp.hpl.hp.com>
In article <3814432C.D55D507B@ife.ee.ethz.ch> on Mon, 25 Oct 1999
13:46:52 +0200, Alex Rhomberg <rhomberg@ife.ee.ethz.ch> says...
+ Larry Rosler wrote:
+ > In article <7uqmm5$end$1@internal-news.uu.net> on 22 Oct 1999
+ > 21:58:29 GMT, Erik van Roode <newsposter@cthulhu.demon.nl> says...
+ >
+ > > use Benchmark;
+ > >
+ > > my $hash1 = ();
+ > > my $hash2 = ();
+ > > for (my $i = 0; $i<10000; $i++) {
+ > > $hash1{$i} = $i % 100;
+ > > $hash2{20000 - $i} = $i % 100;
+ > > }
+ > >
+ > > timethese(100, {
+ > > add1 => sub { my %hash = (%hash1, %hash2); },
+ > > add2 => sub { my %hash = %hash1; @hash{ keys %hash2 } =
+ > > values %hash2; },
+ > > });
+ >
+ > Just a nit, because my tests run like yours. Because you didn't
+ > 'use strict;', you didn't notice that those hash declarations should
+ > be %hashX, not $hashX. Nothing changes, of course.
+
+ Something does change
+ The funny thing is, the tests _only_ work because he my'ed the wrong
+ variables.
+ Benchmark uses the globals, not lexical variables. I found this out,
+ when sorting *huge* arrays was incredibly fast :-)
+ if he changes the my, his benchmark breaks.
Sorry, that is just not true. The Benchmark module makes no distinction
between lexical variables and global variables. Here are my results for
the code above, changed only to declare hashes instead of scalars (so it
now runs with 'use strict;'):
Benchmark: timing 100 iterations of add1, add2...
add1: 29 wallclock secs (28.78 usr + 0.38 sys = 29.16 CPU)
add2: 33 wallclock secs (32.50 usr + 0.59 sys = 33.09 CPU)
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 25 Oct 1999 11:07:55 -0400
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: How can I join two hashes?
Message-Id: <x3y3duza37a.fsf@tigre.matrox.com>
Ron Seifenberger <troja2977@my-deja.com> writes:
> is there a possibility to join two hashes?
you mean create a new hash from the key/value pairs of two separate
hashes? Or update one hash with the key/value pairs of another?
Use a hash slice:
my (%hash1, %hash2);
# now populate the hashes with your favourite
# key/value pairs.
# ...
# Now update %hash1 with the contents of %hash2
@hash1{keys %hash2} = values %hash2;
Note that if %hash1 and %hash2 share some keys, then the values in
%hash1 will be lost.
HTH,
--Ala
PS. Isn't there a FAQ for this? I vaguely remember one, but I can't
find it.
------------------------------
Date: Mon, 25 Oct 1999 13:46:52 +0200
From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
To: Larry Rosler <lr@hpl.hp.com>
Subject: Re: How can I join two hashes?
Message-Id: <3814432C.D55D507B@ife.ee.ethz.ch>
Larry Rosler wrote:
>
> In article <7uqmm5$end$1@internal-news.uu.net> on 22 Oct 1999 21:58:29
> GMT, Erik van Roode <newsposter@cthulhu.demon.nl> says...
>
> ...
>
> > use Benchmark;
> >
> > my $hash1 = ();
> > my $hash2 = ();
> > for (my $i = 0; $i<10000; $i++) {
> > $hash1{$i} = $i % 100;
> > $hash2{20000 - $i} = $i % 100;
> > }
> >
> > timethese(100, {
> > add1 => sub { my %hash = (%hash1, %hash2); },
> > add2 => sub { my %hash = %hash1; @hash{ keys %hash2 } = values %hash2; },
> > });
>
> Just a nit, because my tests run like yours. Because you didn't 'use
> strict;', you didn't notice that those hash declarations should be
> %hashX, not $hashX. Nothing changes, of course.
Something does change
The funny thing is, the tests _only_ work because he my'ed the wrong
variables.
Benchmark uses the globals, not lexical variables. I found this out,
when sorting *huge* arrays was incredibly fast :-)
if he changes the my, his benchmark breaks.
- Alex
------------------------------
Date: Mon, 25 Oct 1999 14:12:17 +0200
From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
Subject: Re: How can I join two hashes?
Message-Id: <38144921.28CA87F@ife.ee.ethz.ch>
Rick Delaney wrote:
> Well, let's see.
>
> #!/usr/local/bin/perl -w
>
> use strict;
> use Benchmark;
> use vars qw(%h1 %h2);
> %h1 = (a => 'b', b => 'a');
> %h2 = (c => 'd', d => 'c');
These hashes are _way_ too small for benchmarking
> timethese(100000, {
> add1 => q { my %hash = (%h1, %h2); },
> add2 => q { my %hash = %h1; @hash{ keys %h2 } = values %h2; },
> add3 => q {
> my %hash = (keys %h1, values %h1, keys %h2, values %h2)
add3 works only for a small set of all possible hashes.
You construct a hash of key1=>key2, key3=>key4....value1=>value2 etc.
The funny thing is, your test hash should work. Makes me wonder if you
wanted to confuse us...
now, let's look at the following benchmarks:
=======================================================================
#!/usr/bin/perl -w
#use 5.005;
use strict;
use Benchmark;
use vars qw/%hash1 %hash2 %hasha %hashb %hashc %hashd/;
$hash1{$_} = rand, $hash2{"i$_"} = rand for (1..10_000);
timethese
(50,
{ concp => q {%hasha = %hash1; %hasha = (%hash1, %hash2)},
concat => q {%hashb = (%hash1, %hash2)},
keyval => q {%hashc = %hash1; @hashc{keys %hash2} = values %hash2},
manual => q {%hashd = %hash1; $hashd{$a} = $b while ($a,$b) = (each
%hash2)}
});
#always good to check the result:
no strict 'refs';
print "@{[map {scalar keys %{'hash'.$_}} ('a'..'d')]}\n";
=======================================================================
Benchmark: timing 50 iterations of concat, concp, keyval, manual...
concat: 10 wallclock secs ( 9.49 usr + 0.03 sys = 9.52 CPU)
concp: 15 wallclock secs (14.92 usr + 0.01 sys = 14.93 CPU)
keyval: 10 wallclock secs (10.37 usr + 0.01 sys = 10.38 CPU)
manual: 12 wallclock secs (11.88 usr + 0.01 sys = 11.89 CPU)
20000 20000 20000 20000
=======================================================================
Conclusions:
- for adding two hashes and storing the result in a third, (%a,%b) is
fastest (concat)
- for adding one hash to another, the slice method is fastest (keyval)
- checking that the benchmarked code actually does anything is
important.
(the "manual" was really fast until I replaced 'for' with 'while' :-)
- Alex
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 1182
**************************************