[26894] in Perl-Users-Digest
Perl-Users Digest, Issue: 8885 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 25 18:05:26 2006
Date: Wed, 25 Jan 2006 15:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 25 Jan 2006 Volume: 10 Number: 8885
Today's topics:
been using Java too long: please help with dynamic "our <rwatkinsNOSPAM@NOSPAMfoo-bar.org>
Re: been using Java too long: please help with dynamic <jgibson@mail.arc.nasa.gov>
Re: been using Java too long: please help with dynamic <rwatkinsNOSPAM@NOSPAMfoo-bar.org>
Re: concatenate variables during looping question <jgibson@mail.arc.nasa.gov>
Copying complex data structure (Hue-Bond)
Re: Copying complex data structure <kkeller-usenet@wombat.san-francisco.ca.us>
Re: Copying complex data structure <xx087@freenet.carleton.ca>
Re: Copying complex data structure <1usa@llenroc.ude.invalid>
Re: Copying complex data structure (Randal L. Schwartz)
Thanks for the comments Jan. 25, 2006 <edgrsprj@ix.netcom.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 25 Jan 2006 19:52:41 GMT
From: Robert Watkins <rwatkinsNOSPAM@NOSPAMfoo-bar.org>
Subject: been using Java too long: please help with dynamic "our" declaration
Message-Id: <Xns975697D69761Drwatkinsfoobarorg@216.77.188.18>
I've been away from Perl too long, folks, and I'm rusty.
Can't figure out how to declare a few dynamically generated names (i.e.
they could change with each run of the script) as "our" vars.
The idea is to use the names as filehandles, and to write to different
files based on what's found:
--------------------------------------------------------
our $name;
*name = *File::Find::name;
// this is the equivalent of what the script might see
// from the dynamic input:
my @statuses = ("New", "Updated", "Commented and Updated");
my $statuses = '(' . join("|", @statuses) . ')';
my $data;
my ($fh, $filename);
foreach $fh (@statuses) {
$us = $fh;
$us =~ s/\s+//g;
$filename = "blah.$us.list";
open($fh, ">$filename") or die "Could not create $filename: $!\n";
}
File::Find::find( {wanted => \&wanted, follow => 1}, CONTENT_DIR);
foreach $fh (@statuses) {
close($fh);
}
sub wanted
{
local $/;
my $data;
if (/^(sect0|withdrawal)-meta\.html\z/s) {
open(FH, "<$name") or warn "Can't open $name: $!\n";
$data = <FH>;
close(FH);
if ($data =~ /<meta name="unitStatus" content="($statuses)">/) {
$fh = $1;
print $fh "$name\n";
}
}
}
--------------------------------------------------------
Any suggestions?
Thanks,
-- Robert
------------------------------
Date: Wed, 25 Jan 2006 12:44:14 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: been using Java too long: please help with dynamic "our" declaration
Message-Id: <250120061244141829%jgibson@mail.arc.nasa.gov>
In article <Xns975697D69761Drwatkinsfoobarorg@216.77.188.18>, Robert
Watkins <rwatkinsNOSPAM@NOSPAMfoo-bar.org> wrote:
> I've been away from Perl too long, folks, and I'm rusty.
>
> Can't figure out how to declare a few dynamically generated names (i.e.
> they could change with each run of the script) as "our" vars.
>
> The idea is to use the names as filehandles, and to write to different
> files based on what's found:
>
> --------------------------------------------------------
> our $name;
> *name = *File::Find::name;
>
> // this is the equivalent of what the script might see
> // from the dynamic input:
> my @statuses = ("New", "Updated", "Commented and Updated");
>
> my $statuses = '(' . join("|", @statuses) . ')';
> my $data;
> my ($fh, $filename);
>
> foreach $fh (@statuses) {
> $us = $fh;
> $us =~ s/\s+//g;
> $filename = "blah.$us.list";
> open($fh, ">$filename") or die "Could not create $filename: $!\n";
> }
>
> File::Find::find( {wanted => \&wanted, follow => 1}, CONTENT_DIR);
>
> foreach $fh (@statuses) {
> close($fh);
> }
>
> sub wanted
> {
> local $/;
> my $data;
> if (/^(sect0|withdrawal)-meta\.html\z/s) {
> open(FH, "<$name") or warn "Can't open $name: $!\n";
> $data = <FH>;
> close(FH);
> if ($data =~ /<meta name="unitStatus" content="($statuses)">/) {
> $fh = $1;
> print $fh "$name\n";
> }
> }
> }
> --------------------------------------------------------
>
> Any suggestions?
Yes. Don't use dynamically generated variables. Use a hash of
filehandles instead:
#!/usr/local/bin/perl
use strict;
use warnings;
my @statuses = ("New", "Updated", "Commented and Updated");
my %files;
foreach my $name (@statuses) {
$name =~ tr/ //d;
my $filename = "blah.$name.list";
open($files{$name}, '>', $filename) or
die "Could not create $filename: $!\n";
}
foreach my $name ( keys %files ) {
print { $files{$name} } "File for $name\n"; # need block around hash
}
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: Wed, 25 Jan 2006 21:32:46 GMT
From: Robert Watkins <rwatkinsNOSPAM@NOSPAMfoo-bar.org>
Subject: Re: been using Java too long: please help with dynamic "our" declaration
Message-Id: <Xns9756A8CEA42F9rwatkinsfoobarorg@216.77.188.18>
Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in news:250120061244141829%
jgibson@mail.arc.nasa.gov:
> Yes. Don't use dynamically generated variables. Use a hash of
> filehandles instead:
>
Brilliant. Thank you. Obviously this is one to remember.
-- RW
------------------------------
Date: Wed, 25 Jan 2006 11:20:24 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: concatenate variables during looping question
Message-Id: <250120061120241419%jgibson@mail.arc.nasa.gov>
In article <43d7b145$0$1765$8b463f8a@news.nationwide.net>, lance-news
<lance-news@augustmail.com> wrote:
> Hey all,
>
> Trying to create a loop and need some help
> in concatenation
>
> My current syntax:
>
> $Sheet->Cells(16,2)->{Value} = $Ja13_1;
> $Sheet->Cells(16,3)->{Value} = $Ja13_2;
> $Sheet->Cells(16,4)->{Value} = $Ja13_3;
> $Sheet->Cells(16,5)->{Value} = $Ja13_4;
> $Sheet->Cells(16,6)->{Value} = $Ja13_5;
> $Sheet->Cells(16,7)->{Value} = $Ja13_6;
>
> $Sheet->Cells(34,2)->{Value} = $Ks13_1;
> $Sheet->Cells(34,3)->{Value} = $Ks13_2;
> $Sheet->Cells(34,4)->{Value} = $Ks13_3;
> $Sheet->Cells(34,5)->{Value} = $Ks13_4;
> $Sheet->Cells(34,6)->{Value} = $Ks13_5;
> $Sheet->Cells(34,7)->{Value} = $Ks13_6;
>
> What I am trying to do:
>
> for (my $i=2; $i=7; $i++){
> $Sheet->Cells(16,$i)->{Value} = "$Ja13_"."$i-1";
> }
>
> but doesn't work.
See perldoc -q "variable as a variable name" for how to do it and why
you shouldn't.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: Wed, 25 Jan 2006 20:58:28 +0000 (UTC)
From: "David Serrano (Hue-Bond)" <responder_solo_en_el_grupo@yahoo.es>
Subject: Copying complex data structure
Message-Id: <slrndtfpjk.tjq.responder_solo_en_el_grupo@genus.hue-bond.info>
I have a complex data structure that I want to copy. I know that if I do:
my $copy = $orig;
All I get is another reference to the original data, not a copy. So I tried:
my $copy = \%{ $orig };
But I came again with the same ref. Then I remembered '{}':
$ perl
my $c={
qq=>'kk',
foo=>'bar',
nested=>{
key=>'val'
}
};
my $d = { %$c };
$d->{'nested'}{'key'}='another val';
print "$c $d\n";
use Data::Dumper; print Dumper $c; print Dumper $d;
__END__
HASH(0x814ab44) HASH(0x816d498)
$VAR1 = {
'qq' => 'kk',
'foo' => 'bar',
'nested' => {
'key' => 'another val'
}
};
$VAR1 = {
'qq' => 'kk',
'foo' => 'bar',
'nested' => {
'key' => 'another val'
}
};
We can see that the refs are not the same, but changing some element in $d
also changed the corresponding one in $c! I suppose that, while $c and $d
are references to different data, both copies of 'nested' still are the
same.
$ perl
my $c={
qq=>'kk',
foo=>'bar',
};
my $d = { %$c };
$d->{'foo'}='kk';
print "$c $d\n";
use Data::Dumper; print Dumper $c; print Dumper $d;
__END__
HASH(0x814ac28) HASH(0x816d3f0)
$VAR1 = {
'qq' => 'kk',
'foo' => 'bar'
};
$VAR1 = {
'qq' => 'kk',
'foo' => 'kk'
};
When there are no references inside the main hash, it's ok.
So, how can I manage to make a copy of all data, no matter how deep the
references get? I thought that one way is by using Storable:
$ perl
my $c={
qq=>'kk',
foo=>'bar',
nested=>{
key=>'val'
}
};
use Storable qw/freeze thaw/;
my $d = thaw freeze $c;
$d->{'nested'}{'key'}='another val';
print "$c $d\n";
use Data::Dumper; print Dumper $c; print Dumper $d;
__END__
HASH(0x814ab44) HASH(0x82378b0)
$VAR1 = {
'qq' => 'kk',
'foo' => 'bar',
'nested' => {
'key' => 'val'
}
};
$VAR1 = {
'qq' => 'kk',
'foo' => 'bar',
'nested' => {
'key' => 'another val'
}
};
But, is there another, "better"(TM) way?
--
David Serrano
------------------------------
Date: Wed, 25 Jan 2006 13:11:57 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: Copying complex data structure
Message-Id: <u56la3xqae.ln2@goaway.wombat.san-francisco.ca.us>
On 2006-01-25, David Serrano (Hue-Bond) <responder_solo_en_el_grupo@yahoo.es> wrote:
> I have a complex data structure that I want to copy. I know that if I do:
It sounds like you want to do a deep copy. Google turned up this oldish
article from Randal:
http://www.stonehenge.com/merlyn/UnixReview/col30.html
It's from 2000, but Randal references it in a column from 2004, so it
can't be *too* off the mark.
I vaguely remember there being a module (other than Storable) that could
do deep copies, but google isn't turning that up.
> use Storable qw/freeze thaw/;
>
> But, is there another, "better"(TM) way?
It seems like you could use Storable's dclone instead.
--keith
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
see X- headers for PGP signature information
------------------------------
Date: 25 Jan 2006 21:28:01 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: Copying complex data structure
Message-Id: <slrndtfrb1.55e.xx087@smeagol.ncf.ca>
At 2006-01-25 03:58PM, David Serrano (Hue-Bond) <responder_solo_en_el_grupo@yahoo.es> wrote:
> I have a complex data structure that I want to copy. I know that if I do:
[...]
Googling for "perl deep copy" reveals:
http://www.stonehenge.com/merlyn/UnixReview/col30.html
http://www.unix.org.ua/orelly/perl/cookbook/ch11_13.htm
--
Glenn Jackman
Ulterior Designer
------------------------------
Date: Wed, 25 Jan 2006 22:11:10 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Copying complex data structure
Message-Id: <Xns9756AEE636399asu1cornelledu@127.0.0.1>
Glenn Jackman <xx087@freenet.carleton.ca> wrote in
news:slrndtfrb1.55e.xx087@smeagol.ncf.ca:
> http://www...ua/orelly/perl/cookbook/ch11_13.htm
Please don't post URLs to pirated material.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 25 Jan 2006 14:50:02 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Copying complex data structure
Message-Id: <868xt3disl.fsf@blue.stonehenge.com>
>>>>> "Keith" == Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> writes:
Keith> It sounds like you want to do a deep copy. Google turned up this oldish
Keith> article from Randal:
Keith> http://www.stonehenge.com/merlyn/UnixReview/col30.html
Keith> It's from 2000, but Randal references it in a column from 2004, so it
Keith> can't be *too* off the mark.
Occasionally, I say something timeless. :)
If I haven't, it goes into the recycling bin for a newer uptake. With 1.5
columns per month to generate, and 240+ already done, it's hard to come up
with new ideas.
print "Just another Perl hacker,"; # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Wed, 25 Jan 2006 19:21:09 GMT
From: "edgrsprj" <edgrsprj@ix.netcom.com>
Subject: Thanks for the comments Jan. 25, 2006
Message-Id: <FcQBf.203$Nv2.78@newsread1.news.atl.earthlink.net>
Thanks for the comments everyone. I am going to have to give some
additional thought to the responses which were posted.
Essentially what I am doing is creating what might be called a
"shadow" operating system which is running behind Windows. It greatly
expands the capabilities of Windows by automating a variety of repetitive
and logical operations. For example, a Perl program can do some complex
calculations and then feed the output to another program which is instructed
to act on the data. Perl can then collect data from that program and do
some additional work.
The SendKeys() command I am now using does not transmit information
to other programs such as text editors very quickly. I am getting around
that by sending longer strings etc. to the Windows clipboard and then having
SendKeys() use a Ctrl V command to paste the information to the text editor
or whatever. That procedure works quite well and makes it possible to run
some of those applications at practical speeds even though they are being
run as though the information is being typed into them by hand. Some of
those programs go back decades in time. And it is impossible to work with
them in any other way.
I believe that I am well aware of the implications of this type of
computer programming. You can create a system which can wreck havoc on your
computer if it starts sending out bad commands or even worse, gets infected
with a virus. However I am being fairly careful in testing the programs I
am developing. And everything important on my computer gets backed up
regularly in case something does go wrong.
The particular Perl application that I am working on right now is
related to mitigating natural disasters and is quite important. I am in
constant touch with other scientists around the world. That effort was
discussed indirectly in a book which was published earlier this month. And
it will probably be discussed in a TV documentary which is presently being
prepared and which I understand is scheduled be seen by people around the
world.
------------------------------
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 8885
***************************************