[26191] in Perl-Users-Digest
Perl-Users Digest, Issue: 8380 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 1 18:05:25 2005
Date: Thu, 1 Sep 2005 15:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 1 Sep 2005 Volume: 10 Number: 8380
Today's topics:
Combining multiple hash references into one hash refere <nomail@sorry.com>
Re: Combining multiple hash references into one hash re xhoster@gmail.com
Re: DOM from XML without C libs? <glex_no-spam@qwest-spam-no.invalid>
How to have perl on a CD (windows) <newskf@numericable.fr>
Re: How to have perl on a CD (windows) <1usa@llenroc.ude.invalid>
Re: How to have perl on a CD (windows) <newskf@numericable.fr>
Re: values of hash of hash <ngoc@yahoo.com>
Re: values of hash of hash <someone@example.com>
Re: values of hash of hash <glex_no-spam@qwest-spam-no.invalid>
Re: values of hash of hash <ngoc@yahoo.com>
Re: values of hash of hash <perl@my-header.org>
Re: values of hash of hash <ngoc@yahoo.com>
Re: values of hash of hash <krevlar.newsgroups@tragetaschen.dyndns.org>
where to get Active perl modules <newskf@numericable.fr>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 01 Sep 2005 13:47:11 -0700
From: Arvin Portlock <nomail@sorry.com>
Subject: Combining multiple hash references into one hash reference
Message-Id: <df7pcj$2187$1@agate.berkeley.edu>
I know lots of ways to combine multiple hashes into a single
hash but I'm very concerned about memory and copy by value.
I'm processing some XML documents and have several thousand
elements that must be linked to relatively few hashes. These
hashes have unique keys among them so I don't have to worry
about one hash element overwriting another with the same
key. The following works as it should:
my $hash1 = {
'key1' => 'Value 1',
'key2' => 'Value 2',
'key3' => 'Value 3',
};
my $hash2 = {
'key4' => 'Value 4',
'key5' => 'Value 5',
'key6' => 'Value 6',
};
my %newhash = (%$hash1, %$hash2);
# The following do not work:
# my $newhash = { $hash1, $hash2 };
# my $newhash = [ $hash1, $hash2 ];
# my %newhash = ( $hash1, $hash2 );
foreach my $key (keys %newhash) {
print "$key: $newhash{$key}\n";
}
But I'm concerned I'm creating copies of each of
these elements for all of the thousands of instances
of %newhash I will be creating. Is there a faster and
memory efficient way to do this?
Thanks!
Arvin
------------------------------
Date: 01 Sep 2005 21:42:50 GMT
From: xhoster@gmail.com
Subject: Re: Combining multiple hash references into one hash reference
Message-Id: <20050901174250.311$ek@newsreader.com>
Arvin Portlock <nomail@sorry.com> wrote:
> I know lots of ways to combine multiple hashes into a single
> hash but I'm very concerned about memory and copy by value.
> I'm processing some XML documents and have several thousand
> elements that must be linked to relatively few hashes. These
> hashes have unique keys among them so I don't have to worry
> about one hash element overwriting another with the same
> key. The following works as it should:
>
> my $hash1 = {
> 'key1' => 'Value 1',
> 'key2' => 'Value 2',
> 'key3' => 'Value 3',
> };
>
> my $hash2 = {
> 'key4' => 'Value 4',
> 'key5' => 'Value 5',
> 'key6' => 'Value 6',
> };
>
> my %newhash = (%$hash1, %$hash2);
>
> # The following do not work:
> # my $newhash = { $hash1, $hash2 };
> # my $newhash = [ $hash1, $hash2 ];
> # my %newhash = ( $hash1, $hash2 );
All of those work. They do exactly what they should do, even if that is
not what you want them to do.
Maybe this is more to your liking:
my $newhash = { %$hash1, %$hash2 };
>
> foreach my $key (keys %newhash) {
> print "$key: $newhash{$key}\n";
> }
Presumably, that isn't all you are doing, because if it were you would
just use two loops, one for hash1 and one for hash2, and never make the
combined hash in the first place. And not making the combined hash in the
first place is, of course, the best solution if you can get away with it.
If you need more generalized than just those two hashes, then use an AoH
with nested loop for printing.
>
> But I'm concerned I'm creating copies of each of
> these elements for all of the thousands of instances
> of %newhash I will be creating.
Will $hash1 and $hash2 go out of scope or get redefined shortly after
%newhash (or $newhash) is created from them? If so, you most likely
needn't worry on the memory front. And will all these thousands of
instances of %newhash also be properly scoped?
> Is there a faster and
> memory efficient way to do this?
Is this micro-optimization week or something?
Would it be acceptable to add %$hash2 into %$hash1 rather than making
a brand new %newhash? If so,
@{$hash1}{keys %$hash2}=values %$hash2;
is somewhat more memory efficient.
If not, then:
my %newhash=%$hash1;
undef $hash1;
@newhash{keys %$hash2}=values %$hash2;
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Thu, 01 Sep 2005 10:20:11 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: DOM from XML without C libs?
Message-Id: <M_ERe.15$0b1.371@news.uswest.net>
bugbear wrote:
> Question: can I install XML::Handler::BuildDOM without
> installing XML::Parser?
What happened when you tried it?
------------------------------
Date: Thu, 01 Sep 2005 20:21:31 +0200
From: News KF <newskf@numericable.fr>
Subject: How to have perl on a CD (windows)
Message-Id: <43174685$0$164$a3f2974a@nnrp1.numericable.fr>
Hi,
I want to have a perl-application, that could reside on a CD or USB
memory stick and could run on any windows computer.
Source code protection is of now issue, but being able to run the code
wherever I go would be important.
Is there any standard recipe to do so?
What I magine is to write a perl script, that needs some modules with
native code sections for example GD or expat.
Then I'd like to create a directory containing the perl interpreter and
all required DLLs and modules and the source code.
Is it possible get something like this up and running without messing
around in the registry or any other file on the hard disk?
Thanks inadvance for any info and bye
nkf
P.S. My perl experience is so far only linux and cygwin based and I
don't know about how messy it is to install perl on windows.
------------------------------
Date: Thu, 1 Sep 2005 19:02:01 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to have perl on a CD (windows)
Message-Id: <Xns96C498F47FBA9asu1cornelledu@132.236.56.8>
News KF <newskf@numericable.fr> wrote in news:43174685$0$164
$a3f2974a@nnrp1.numericable.fr:
> I want to have a perl-application, that could reside on a CD or USB
> memory stick and could run on any windows computer.
> Source code protection is of now issue, but being able to run the code
> wherever I go would be important.
>
>
> Is there any standard recipe to do so?
http://search.cpan.org/dist/PAR/
Sinan
------------------------------
Date: Thu, 01 Sep 2005 22:36:54 +0200
From: News KF <newskf@numericable.fr>
Subject: Re: How to have perl on a CD (windows)
Message-Id: <43176640$0$170$a3f2974a@nnrp1.numericable.fr>
Hi Thanks For this info,
I'll look into this later in detail.
I had already a quick glance.
What I figured out by myself is, that the zip file of active perl can be
expanded onto a memory stick and it seems, that I can execute perl
scripts by setting the pathname in a batch file.
This solution should be fine for me for the time being.
On the other hand:
PAR seems to be quite attractive as the result should be just one exe
and not a whole directory tree.
Now my next question is where to get the apropriate modules for Active
perl. (I started a new thread for this).
Thanks again and bye
nkf
A. Sinan Unur wrote:
> News KF <newskf@numericable.fr> wrote in news:43174685$0$164
> $a3f2974a@nnrp1.numericable.fr:
>
>
>>I want to have a perl-application, that could reside on a CD or USB
>>memory stick and could run on any windows computer.
>>Source code protection is of now issue, but being able to run the code
>>wherever I go would be important.
>>
>>
>>Is there any standard recipe to do so?
>
>
> http://search.cpan.org/dist/PAR/
>
> Sinan
------------------------------
Date: Thu, 01 Sep 2005 16:51:01 +0200
From: ngoc <ngoc@yahoo.com>
Subject: Re: values of hash of hash
Message-Id: <4317225c$1@news.broadpark.no>
Yes. I have considered it. My data is really complex. Each project have
many employees, each employee have many tasks, each task have time,
budget etc... This can be model as 3 level hash as I did.
For another reason, 3 nested for loop is not beautiful programming. I
try to avoid.
So I am looking for simple solution without using 3 for loop.
An analogue to it, 'map' and 'grep' can do 5 lines of code to 1 line.
xhoster@gmail.com wrote:
> ngoc <ngoc@yahoo.com> wrote:
>
>>I have
>>$hash{1}{2}{3} = 7;
>>$hash{1}{4}{5} = 6;
>>I want to get 7 and 6 without using three for loop and keys function
>>How can I do it?
>
>
> Have you considered not creating the uselessly nested hashes in the first
> place, but rather sticking the data into a more appropriate structure?
>
> Since you, for completely unknown reasons, don't want to use foreach loops
> or the keys function, it would help tremendously if you would list all the
> other features you irrationally wish to avoid.
>
> Xho
>
------------------------------
Date: Thu, 01 Sep 2005 16:12:56 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: values of hash of hash
Message-Id: <cMFRe.212418$9A2.114648@edtnps89>
ngoc wrote:
> I have
> $hash{1}{2}{3} = 7;
> $hash{1}{4}{5} = 6;
> I want to get 7 and 6 without using three for loop and keys function
> How can I do it?
You could always use the old style for multiple keys:
$hash{1,2,3} = 7;
$hash{1,4,5} = 6;
print for values %hash;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 01 Sep 2005 11:24:02 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: values of hash of hash
Message-Id: <DWFRe.24$0b1.672@news.uswest.net>
ngoc wrote:
** If you continue to top-post, a lot of folks will add you to their
kill-file. **
> Yes. I have considered it. My data is really complex. Each project have
> many employees, each employee have many tasks, each task have time,
> budget etc... This can be model as 3 level hash as I did.
Not terribly complex. Sounds like you'd be better off using a database.
(select distinct(x) from table). Possibly SQLite:
http://www.hwaci.com/sw/sqlite/ might be a "beautiful" solution.
> For another reason, 3 nested for loop is not beautiful programming. I
> try to avoid.
Then don't build a HoHoH data structure.
> So I am looking for simple solution without using 3 for loop.
Then use a different data structure. Possibly, when building
the HoHoH, build another one to hold the values you're after.
> An analogue to it, 'map' and 'grep' can do 5 lines of code to 1 line.
Put them all on one line, or put the 3 for's in a sub or method, then
you can make a "beautiful" subroutine call.
------------------------------
Date: Thu, 01 Sep 2005 20:12:47 +0200
From: ngoc <ngoc@yahoo.com>
Subject: Re: values of hash of hash
Message-Id: <431751a6$1@news.broadpark.no>
J. Gleixner wrote:
> ngoc wrote:
>
> ** If you continue to top-post, a lot of folks will add you to their
> kill-file. **
What is top-post? I do not know what it means.
>
>> Yes. I have considered it. My data is really complex. Each project
>> have many employees, each employee have many tasks, each task have
>> time, budget etc... This can be model as 3 level hash as I did.
>
>
> Not terribly complex. Sounds like you'd be better off using a database.
> (select distinct(x) from table). Possibly SQLite:
> http://www.hwaci.com/sw/sqlite/ might be a "beautiful" solution.
>
>> For another reason, 3 nested for loop is not beautiful programming. I
>> try to avoid.
>
>
> Then don't build a HoHoH data structure.
>
>> So I am looking for simple solution without using 3 for loop.
>
>
> Then use a different data structure. Possibly, when building
> the HoHoH, build another one to hold the values you're after.
>
>> An analogue to it, 'map' and 'grep' can do 5 lines of code to 1 line.
>
>
> Put them all on one line, or put the 3 for's in a sub or method, then
> you can make a "beautiful" subroutine call.
------------------------------
Date: Thu, 01 Sep 2005 21:14:27 +0200
From: Matija Papec <perl@my-header.org>
Subject: Re: values of hash of hash
Message-Id: <rtjeh1dscapd2sa2us4p64dk21f7amr13g@4ax.com>
X-Ftn-To: ngoc
ngoc <ngoc@yahoo.com> wrote:
>My problem is, I have to compare data from two projects and find out
>duplicated data. Two projects presented as two hash. If first level key
>is different (using exist function), put the whole data attached to it
>in an array. Currently, I have to access level 2, level 3 keys before
>getting it's real value. It is not advanced computing. So I want to
>access all values without using keys. Using "values %hash" just gives me
>next level hash.
This is basically the same what Anno wrote,
my @arr = map values %$_, map values %$_, values %hash;
but what could you practically do only with values? Perhaps you're
approaching the problem from the wrong angle so what seems like solution
only produces new problems?
--
Matija
------------------------------
Date: Thu, 01 Sep 2005 22:23:50 +0200
From: ngoc <ngoc@yahoo.com>
Subject: Re: values of hash of hash
Message-Id: <4317705d$1@news.broadpark.no>
Matija Papec wrote:
>
> This is basically the same what Anno wrote,
>
> my @arr = map values %$_, map values %$_, values %hash;
Yes it looks better.
>
> but what could you practically do only with values? Perhaps you're
> approaching the problem from the wrong angle so what seems like solution
> only produces new problems?
>
>
I wrote a perl application for 2 years ago, when I was right out from
school. It is 8000 lines of code. After 2 years, I get more experience
and knowledge. So I try to reduce amount of code and refine it. What
application do, is comparing data from two databases. Data is hierarchic:
Each project have many employees, each employee have many tasks, each
task have time, budget etc... So I did
$data_1{$proj_id}{$employee}{$task} = [$id, $employee, $task,
$start_time, $budget,.........];
$data_2{$proj_id}{$employee}{$task} = [$id, $employee, $task,
$start_time, $budget,.........];
I first compare $proj_id, using exist function. If $proj_id from data_1
not found in data_2, I store it into another hash $unique_hash{$proj_id}
= $data_1{$proj_id}. Later, I want to display all data, I have to use 3
for loop.
foreach (keys %unique_hash) {
foreach (keys %{$unique_hash{$_}}) {
foreach (keys %{$unique_hash{$_}{$_}}) {
$values = $unique_hash{$_}{$_}{$_};
}
}
}
Reading from many computer science books, a good programming style is
not many nested for loop.
Looking back to my app. I use 3 for loop, in many places in my app. So I
have to change it, IF I WANT TO KEEP my job :-). So I posted to forum
in hope some smart guys out there can help me to be better programmer.
(And they can also learn new cases too).
------------------------------
Date: Fri, 02 Sep 2005 00:01:54 +0200
From: Arne Ruhnau <krevlar.newsgroups@tragetaschen.dyndns.org>
Subject: Re: values of hash of hash
Message-Id: <df7toj$1el$04$1@news.t-online.com>
ngoc wrote:
> I have
> $hash{1}{2}{3} = 7;
> $hash{1}{4}{5} = 6;
> I want to get 7 and 6 without using three for loop and keys function
> How can I do it?
You could use the following code, which "just hides the loops". The code is
taken out of some more general version I am developing and thus contains
more features than necessary (and it works, as it is, only if you specify
an empty pattern to look for...). Hm.
Graphsearch::matcherFor takes a nested hash as argument and returns an
object/factory/coderef capable of understanding a "Pattern" and returning
an object/Iterator/coderef that extracts every path in the hash matching
the pattern.
BTW: Note the evil goto ;)
<code>
use strict;
use warnings;
my %hash;
$hash{1}{2}{3} = 7;
$hash{1}{4}{5} = 6;
my $matcher = Graphsearch::matcherFor(\%hash);
my $iter = $matcher->([qw//]); # [qw//] is the empty pattern
while(my $path = $iter->()) {
print $path->[-1]; # $path contains [1,2,3,7] and [1,4,5,6]
}
package Graphsearch;
use strict;
use warnings;
sub matcherFor {
my $data = shift;
my $possiblePath = sub { return 0 unless defined ($_[1]);
exists $_[0]->{$_[1]}
};
my $isTerminal = sub { !ref($_[0]->{$_[1]}) };
my $walkpath = sub { $_[0]->{$_[1]} };
my $defaultAction = sub { [keys %{+shift}] };
return sub {
my ($pattern, $requireLength) = @_;
$requireLength ||=0;
my $plength = scalar(@$pattern)-1;
my ($stacks, $olddata, $path, $patternpos, $backtrackfirst)
= ([], [], [], 0, 0);
my ($try, $lengthOk, $nextTry, $consumePattern, $backtrack, $get,
$nextMove);
$lengthOk = sub {
return 1 unless $requireLength; $plength < $patternpos
};
$nextTry = sub {
$try = pop @{$stacks->[-1]}
};
$consumePattern = sub {
my $action = $pattern->[$patternpos] || $defaultAction;
push @$stacks, $action->($data);
++$patternpos;
goto $nextTry;
};
$nextMove = sub {
goto defined($nextTry->())?$get:$backtrack
};
$backtrack = sub {
return undef unless(scalar(@$olddata));
pop @$stacks;
pop @$path;
$data = pop(@$olddata);
--$patternpos;
goto $nextMove;
};
$get = sub {
if ($backtrackfirst or !$possiblePath->($data, $try)) {
$backtrackfirst = 0;
goto $nextMove;
}
else {
if ($isTerminal->($data, $try)) {
if($lengthOk->()) {
$backtrackfirst = 1;
return [@$path, $try, $data->{$try}];
}
else { goto $nextMove }
}
else {
push @$olddata, $data;
push @$path, $try;
$data = $walkpath->($data, $try);
$consumePattern->();
goto $get;
}
}
};
$consumePattern->();
return bless $get, __PACKAGE__;
};
}
__END__
------------------------------
Date: Thu, 01 Sep 2005 23:44:10 +0200
From: News KF <newskf@numericable.fr>
Subject: where to get Active perl modules
Message-Id: <43177605$0$167$a3f2974a@nnrp1.numericable.fr>
Hi,
I downloaded the active perl release und unzipped it onto a memory stick.
I can now execute my perl script as desired.
I wanted do download a precompiled version of the module Rec::Descent,
which I can also find on the Active perl web site under
http://aspn.activestate.com/ASPN/Modules/
I did also find Parse::RecDescent
http://aspn.activestate.com/ASPN/Modules/dist_html?dist_id=9430
but I see only documentation, but no place where I can download the library.
Is it possible to donwload single packages and unzip them wherever I
like to unzip them (on the memory stick such, that I have a
transportable perl with all required packages.
thanks in advance and bye
nkf
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 8380
***************************************