Ask a Question related to PERL Beginners, Design and Development.
-
Douglas Houston #1
How do you dynamically assign array names?
Hi,
I am trying to initialize a dynamically-named array, i.e. with the
following test code (if I type "script.pl char" at the command prompt) I
get the message "array char contains 1 2 3" but I get a warning if 'use
strict' is on. Can anyone show me how it should be done (I would like to
use 'use strict'!)?
#!/usr/bin/perl -w
#use strict;
my ($string);
while (<$ARGV[0]>) {
chomp($string=$_);
@$string = (1,2,3); #Won't work if 'use strict' pragma is used
}
print "array $string contains @$string\n";
Douglas Houston Guest
-
Assign Array with Dynamic Variable Names
I have an 2 dimensional array that I want to assign to an 1 dimensional array with a dynamic Variable Name. I can set the newarray function, but I... -
Getting the best answer(WAS: How do you dynamically assign array names?)
I think this touches on an issue that people should consider when they post to this list. Often people ask a question, and instead of asking for... -
#26126 [NEW]: You cannot assign recursive array references.
From: bens at effortlessis dot com Operating system: All PHP version: 4.3.2 PHP Bug Type: Feature/Change Request Bug... -
dynamically assign a member to a channel in the score
Hi, Is it possible to have a member in the cast and put that member onto the stage dynamically through lingo. Currently I have 5 buttons and I... -
global array, can't assign values from variables
Hi I'm just going to give the code and output. It should be self explanatory. The array, $criteria, is having the issue. I don't know what it's... -
Jeff 'Japhy' Pinyan #2
Re: How do you dynamically assign array names?
On Nov 14, Douglas Houston said:
You need to explain WHY you want to do this. There doesn't seem to me to>I am trying to initialize a dynamically-named array, i.e. with the
>following test code (if I type "script.pl char" at the command prompt) I
>get the message "array char contains 1 2 3" but I get a warning if 'use
>strict' is on. Can anyone show me how it should be done (I would like to
>use 'use strict'!)?
be a good reason. Use a hash of array references. Don't turn off strict.
#!/usr/bin/perl
use strict;
use warnings; # perl5.6+
my %data;
my $name = shift;
$data{$name} = [1,2,3];
# or
# @{ $data{$name} } = (1,2,3);
And your "while (<$ARGV[0]>)" is weird, and not working why you think it
works.
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Randal L. Schwartz #3
Re: How do you dynamically assign array names?
>>>>> "Douglas" == Douglas Houston <douglas@davapc1.bioch.dundee.ac.uk> writes:
Douglas> I am trying to initialize a dynamically-named array,
Douglas> i.e. with the following test code (if I type "script.pl char"
Douglas> at the command prompt) I get the message "array char contains
Douglas> 1 2 3" but I get a warning if 'use strict' is on. Can anyone
Douglas> show me how it should be done (I would like to use 'use
Douglas> strict'!)?
It should be done by not doing it. Do not use data to define metadata (like
variable names). Down that path lies madness. "use strict" is trying
to keep you from going mad.
Probably what you are looking for is a hash containing arrayrefs,
pointing at your real data.
--
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!
Randal L. Schwartz Guest
-
Wiggins D Anconia #4
Re: How do you dynamically assign array names?
Well that is kind of the point, you don't :-). That is specifically what> Hi,
>
> I am trying to initialize a dynamically-named array, i.e. with the
> following test code (if I type "script.pl char" at the command prompt) I
> get the message "array char contains 1 2 3" but I get a warning if 'use
> strict' is on. Can anyone show me how it should be done (I would like to
> use 'use strict'!)?
>
> #!/usr/bin/perl -w
> #use strict;
> my ($string);
>
> while (<$ARGV[0]>) {
> chomp($string=$_);
> @$string = (1,2,3); #Won't work if 'use strict' pragma is used
> }
>
> print "array $string contains @$string\n";
>
>
the 'refs' part of 'strict' prevents (symbolic references), for good reason.
perldoc strict
In newer Perls you can shut off that portion of the stricture lexically,
or a better idea is to store a reference to an anonymous array to a
hash, then use the hash key as its name.
my %hash;
$hash{'array_name'} = [ 'one','two','three' ];
print $hash{'array_name'}->[2];
perldoc perlreftut
perldoc perlref
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
Douglas Houston #5
Re: How do you dynamically assign array names?
On Fri, 14 Nov 2003, Jeff 'japhy' Pinyan wrote:
WHY do I need to explain why I want to do this? There certainly isn't a> On Nov 14, Douglas Houston said:
>>> >I am trying to initialize a dynamically-named array, i.e. with the
> >following test code (if I type "script.pl char" at the command prompt) I
> >get the message "array char contains 1 2 3" but I get a warning if 'use
> >strict' is on. Can anyone show me how it should be done (I would like to
> >use 'use strict'!)?
> You need to explain WHY you want to do this. There doesn't seem to me to
> be a good reason. Use a hash of array references. Don't turn off strict.
>
good reason to do it with the test code I posted. If there's NEVER a good
reason, what are the alternatives?
Can you> And your "while (<$ARGV[0]>)" is weird, and not working why you think it
> works.
>
a) explain how I think it works, and
b) explain why it really works.
Douglas Houston Guest
-
Jeff 'Japhy' Pinyan #6
Re: How do you dynamically assign array names?
On Nov 14, Douglas Houston said:
You need to explain why you want to do it is because it can create>On Fri, 14 Nov 2003, Jeff 'japhy' Pinyan wrote:
>>>> On Nov 14, Douglas Houston said:
>>>>>> >I am trying to initialize a dynamically-named array, i.e. with the
>> >following test code (if I type "script.pl char" at the command prompt) I
>> >get the message "array char contains 1 2 3" but I get a warning if 'use
>> >strict' is on. Can anyone show me how it should be done (I would like to
>> >use 'use strict'!)?
>> You need to explain WHY you want to do this. There doesn't seem to me to
>> be a good reason. Use a hash of array references. Don't turn off strict.
>WHY do I need to explain why I want to do this? There certainly isn't a
>good reason to do it with the test code I posted. If there's NEVER a good
>reason, what are the alternatives?
problems down the line. As for an alternative, I told you and showed you:
use a hash of array references.
$hash{foo} = [10, 20, 30];
print $hash{foo}[2]; # 30
I don't know how you think it works, I'm just quite sure that you're using>>> And your "while (<$ARGV[0]>)" is weird, and not working why you think it
>> works.
>a) explain how I think it works, and
that construct because you think it is a standard thing to do, and it's
not. How do YOU think it works? What if the first argument to your
program was "foo*"?
Perl thinks you're doing a file glob. <XXX> is a read from a filehandle>b) explain why it really works.
unless XXX is something more complex than an identifier or a simple
scalar. Thus, <$foo[$x]> is a file glob, glob($foo[$x]). If $foo[$x] has
any wildcards or such, the shell expands that glob to all the files
matching the pattern. But if it's just 'blah', glob('blah') returns
'blah'. Then, the next time, it returns undef, because it's done
returning them.
If that sounds complex, that's because it IS. Doing what you did works in
some cases, for a bizarre reason.
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Charles K. Clarkson #7
RE: How do you dynamically assign array names?
Douglas Houston [mailto:douglas@davapc1.bioch.dundee.ac.uk]
:
: WHY do I need to explain why I want to do this?
Because no one wants to give a loaded gun to
someone who hasn't demonstrated a good grasp of
gun safety.
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
Charles K. Clarkson Guest
-
Wiggins D Anconia #8
RE: How do you dynamically assign array names?
That is a very ironic response coming from someone who based on their> Douglas Houston [mailto:douglas@davapc1.bioch.dundee.ac.uk]
> :
> : WHY do I need to explain why I want to do this?
>
> Because no one wants to give a loaded gun to
> someone who hasn't demonstrated a good grasp of
> gun safety.
>
>
sig I assume is in the US, going to someone based on their address
appears to be in the UK... who'd a thunk it...
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
Steve Grazzini #9
Re: How do you dynamically assign array names?
On Fri, Nov 14, 2003 at 04:30:05PM +0000, Douglas Houston wrote:
Probably Jeff was being polite, giving you the benefit of the doubt,> On Fri, 14 Nov 2003, Jeff 'japhy' Pinyan wrote:>>> On Nov 14, Douglas Houston said:
>>>>>>> I am trying to initialize a dynamically-named array
>> You need to explain WHY you want to do this. There doesn't seem to me to
>> be a good reason. Use a hash of array references. Don't turn off strict.
> WHY do I need to explain why I want to do this? There certainly isn't a
> good reason to do it with the test code I posted.
allowing you to remain innocent-until-proven-guilty, etc. The fact is,
there are very few good reasons to use symrefs -- exporting symbols is
the only one I can think of -- and if you have a good reason, you'll need
to give us some more information.
It's not that there's "NEVER" a good reason to use symbolic references.> If there's NEVER a good reason,
It's just that the code you posted doesn't have one. :-)
Use a hash of array references. Instead of this:> what are the alternatives?
@$name = (1,2,3); # gack! what if $name is "_" or "INC"?
use this.
$hash{$name} = [1,2,3];
I don't know what you think the angle-brackets do here, but...>>> And your "while (<$ARGV[0]>)" is weird, and not working why you think
>> it works.
> Can you
>
> a) explain how I think it works, and
Unless you think they do filename globbing, that code isn't working the> b) explain why it really works.
way you think it works.
--
Steve
Steve Grazzini Guest
-
Rob Dixon #10
Re: How do you dynamically assign array names?
Douglas Houston wrote:
Jeff's pretty sharp. Believe him. And don't be rude to people if you're>
> On Fri, 14 Nov 2003, Jeff 'japhy' Pinyan wrote:
>>> > On Nov 14, Douglas Houston said:
> >> >> > >I am trying to initialize a dynamically-named array, i.e. with the
> > >following test code (if I type "script.pl char" at the command prompt) I
> > >get the message "array char contains 1 2 3" but I get a warning if 'use
> > >strict' is on. Can anyone show me how it should be done (I would like to
> > >use 'use strict'!)?
> > You need to explain WHY you want to do this. There doesn't seem to me to
> > be a good reason. Use a hash of array references. Don't turn off strict.
> >
> WHY do I need to explain why I want to do this? There certainly isn't a
> good reason to do it with the test code I posted. If there's NEVER a good
> reason, what are the alternatives?
asking for a favour.
How can anyone suggest what the 'alternatives' may be without an
explanation of the problem?
Rob
Rob Dixon Guest
-
Rob Dixon #11
Re: How do you dynamically assign array names?
Wiggins D Anconia wrote:
I would have thought it were more ironic the other way>>> > Douglas Houston [mailto:douglas@davapc1.bioch.dundee.ac.uk]
> > :
> > : WHY do I need to explain why I want to do this?
> >
> > Because no one wants to give a loaded gun to
> > someone who hasn't demonstrated a good grasp of
> > gun safety.
> >
> >
> That is a very ironic response coming from someone who based on their
> sig I assume is in the US, going to someone based on their address
> appears to be in the UK... who'd a thunk it...
around. Everybody expects an American gun slinger to
threaten a peaceful Dundee land owner.
Totally :-)))))) No flames thank you.
Rob
Rob Dixon Guest
-
David #12
Re: How do you dynamically assign array names?
Douglas Houston wrote:
this normally can't be done cleanly and is generally not recommanded given> Hi,
>
> I am trying to initialize a dynamically-named array, i.e. with the
> following test code (if I type "script.pl char" at the command prompt) I
> get the message "array char contains 1 2 3" but I get a warning if 'use
> strict' is on. Can anyone show me how it should be done (I would like to
> use 'use strict'!)?
>
> #!/usr/bin/perl -w
> #use strict;
> my ($string);
>
> while (<$ARGV[0]>) {
> chomp($string=$_);
> @$string = (1,2,3); #Won't work if 'use strict' pragma is used
> }
>
> print "array $string contains @$string\n";
there are other ways to accomplish bascially the same thing. but if you are
here to see if it can be done at all, you might want to try something like:
#!/usr/bin/perl -w
use strict;
my $name = shift;
eval <<CODE;
no strict;
@ $name=1..3;
print "\$_\\n" for(@ $name);
CODE
#--
#-- print it again after eval
#--
while(my($k,$v) = each %::){
next unless($k =~ /^$name$/);
local *sym = $v;
print join("\n",@main::sym),"\n";
}
__END__
prints:
1
2
3
1
2
3
david
--
$_='015001450154015401570040016701570162015401440'
,*,=*|=*_,split+local$";map{~$_&1&&{$,<<=1,$#.=qq~
/63968e72w28@_[$_..$||3])=>~}}0..s,.,,g-01;*_=*#,s
[[s/eval+w/.`9`/e]]n\\xng.print+eval(eval"qq`$_`")
David Guest
-
Steve Grazzini #13
Re: How do you dynamically assign array names?
On Fri, Nov 14, 2003 at 12:25:41PM -0800, david wrote:
Sigh... :-)> Douglas Houston wrote:>>>
>> I am trying to initialize a dynamically-named array
> this normally can't be done cleanly and is generally not recommanded given
> there are other ways to accomplish bascially the same thing. but if you are
> here to see if it can be done at all, you might want to try something like:
>
> #!/usr/bin/perl -w
> use strict;
>
> my $name = shift;
>
> eval <<CODE;
> no strict;
> @ $name=1..3;
> print "\$_\\n" for(@ $name);
> CODE
The point is not that *symrefs* are inherently bad. The point is that
turning data into variable names can cause huge, nightmarish maintenance
problems. Even though your code doesn't have any symbolic references,
it can still cause all the same maintenance problems... and it's so much
uglier than just using symrefs in the first place!
For that matter, you can set the values by munging the symbol table:> while(my($k,$v) = each %::){
> next unless($k =~ /^$name$/);
> local *sym = $v;
> print join("\n",@main::sym),"\n";
> }
use strict;
my $name = 'foo';
$::{$name} = [1,2,3]; # LHS is a glob
print "@{ $::{$name} }\n";
no strict 'vars';
print "@foo\n";
But why use the symbol table at all? That's more dangerous than using
a regular hash, and more awkward than using the symbolic references.
--
Steve
Steve Grazzini Guest
-
Steve Grazzini #14
Re: How do you dynamically assign array names?
On Fri, Nov 14, 2003 at 01:24:53PM -0800, david wrote:
Yeah...> David wrote:>>>> this normally can't be done cleanly and is generally not recommanded
>>> given there are other ways to accomplish bascially the same thing.
> Steve Grazzini wrote:>>> But why use the symbol table at all? That's more dangerous than using
>> a regular hash, and more awkward than using the symbolic references.
> didn't i already mention that? :-)
There's just a long tradition of telling people how to use eval() and %::
to get strict-safe-symrefs, and an equally long tradition of smacking whomever
does it. :-)
--
Steve
Steve Grazzini Guest
-
David #15
Re: How do you dynamically assign array names?
David wrote:
Steve Grazzini wrote:>> this normally can't be done cleanly and is generally not recommanded
>> given there are other ways to accomplish bascially the same thing.
didn't i already mention that? :-)>
> But why use the symbol table at all? That's more dangerous than using
> a regular hash, and more awkward than using the symbolic references.
>
david
--
$_='015001450154015401570040016701570162015401440'
,*,=*|=*_,split+local$";map{~$_&1&&{$,<<=1,$#.=qq~
/63968e72w28@_[$_..$||3])=>~}}0..s,.,,g-01;*_=*#,s
[[s/eval+w/.`9`/e]]n\\xng.print+eval(eval"qq`$_`")
David Guest
-
Rob Dixon #16
Re: How do you dynamically assign array names?
Steve Grazzini wrote:
Thanks Steve. I agree>
> On Fri, Nov 14, 2003 at 12:25:41PM -0800, david wrote:>> > Douglas Houston wrote:> >> >>
> >> I am trying to initialize a dynamically-named array
> > this normally can't be done cleanly and is generally not recommanded given
> > there are other ways to accomplish bascially the same thing. but if you are
> > here to see if it can be done at all, you might want to try something like:
> >
> > #!/usr/bin/perl -w
> > use strict;
> >
> > my $name = shift;
> >
> > eval <<CODE;
> > no strict;
> > @ $name=1..3;
> > print "\$_\\n" for(@ $name);
> > CODE
> Sigh... :-)
>
> The point is not that *symrefs* are inherently bad. The point is that
> turning data into variable names can cause huge, nightmarish maintenance
> problems. Even though your code doesn't have any symbolic references,
> it can still cause all the same maintenance problems... and it's so much
> uglier than just using symrefs in the first place!
There's nothing wrong with 'pass by name' code parameters
as such. Algol 60 used to do it as a matter of course, but guess why
it was dropped? For the same reason that "use strict 'refs'"
prohibits it.
Rob
Rob Dixon Guest
-
Drieux #17
Localization problems - Re: How do you dynamically assign array names?
On Friday, Nov 14, 2003, at 09:07 US/Pacific, Wiggins d Anconia wrote:p0: having been a card carrying member of the>>> Douglas Houston [mailto:douglas@davapc1.bioch.dundee.ac.uk]
>> :
>> : WHY do I need to explain why I want to do this?
>>
>> Because no one wants to give a loaded gun to
>> someone who hasn't demonstrated a good grasp of
>> gun safety.
> That is a very ironic response coming from someone who based on their
> sig I assume is in the US, going to someone based on their address
> appears to be in the UK... who'd a thunk it...
Haverfordwest Pistol Club, dyfed wales, allow me
to underscore the importance of firearm safety
in both the USA and the UK, and that contrary
to some failures to comprehend, the reasons actually
are independent of Nation of Origin, passport,
gene pool number, etc, etc, etc. So when we plan
to have these moments, remember what Uncle Drieux would say:
Don't Make ME stop this armoured column and come back there...
that having been said, let us return to the topic.
p1: the messy bit in all of this is the OP's initial
assertion
"I am trying to initialize a dynamically-named array"
and provided an illustration that makes us all
jump into the slit trenches screaming
PSYCHO ALERT!!!
And some of us because
a. we did that to ourselves
b. we had to UnDO that from someone else
at which point we arrive at the counter question:
"WHY do I need to explain why I want to do this?
There certainly isn't a good reason to do it with
the test code I posted. If there's NEVER a good
reason, what are the alternatives?"
And that drives us back to the basic problem,
What exactly ARE you trying to solve here?
At one end of the issue is the question of getting
command line arguments IN,
cf perldoc GetOpt::Long
# because sooner or later you will need the --WingDingDing
# types of command line options
Then there is that other type of Issue with how
to do configuration files, and getting stuff in
from a config file
cf:
<http://www.wetware.com/drieux/PR/blog2/Code/200311.html#id3151675135>
as one of several problems.
then there are the possible issues with using anonymouse arrays,
hashes, subs, and the general issues of referencing and dereferencing..
So yeah, it would sorta help us help you with what Exactly IS one
really trying to do....
p3: and while we are wandering out the door,
try to remember boys and girls, that from
San Francisco and Hawai'i one has to go
to the wild wild east to find 'cowboys',
so let's Put the Six Guns away and get
back on the Big Bus....
ciao
drieux
---
Drieux Guest
-
Randal L. Schwartz #18
Re: How do you dynamically assign array names?
>>>>> "Charles" == Charles K Clarkson <cclarkson@htcomp.net> writes:
Charles> : WHY do I need to explain why I want to do this?
Charles> Because no one wants to give a loaded gun to
Charles> someone who hasn't demonstrated a good grasp of
Charles> gun safety.
Well said!
It's like you've asked "How do I remove this screw with a hammer?"
You're going to get a lot of strange looks, and a bunch of "don't do
that", and a few people will hand you a screwdriver instead.
You don't get to ask the question blindly. We're watching out
for your actual intent and continued success.
--
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!
Randal L. Schwartz Guest
-
R. Joseph Newton #19
Re: How do you dynamically assign array names?
Douglas Houston wrote:
Because you are asking people to take valuable time to help you.> On Fri, 14 Nov 2003, Jeff 'japhy' Pinyan wrote:
>>> > On Nov 14, Douglas Houston said:
> >> >> > >I am trying to initialize a dynamically-named array,
> > You need to explain WHY you want to do this. There doesn't seem to me to
> > be a good reason. Use a hash of array references. Don't turn off strict.
> >
> WHY do I need to explain why I want to do this?
Joseph
R. Joseph Newton Guest
-
R. Joseph Newton #20
Re: How do you dynamically assign array names? [slightly OT: enabling folly]
Wiggins d Anconia wrote:
Yeah. Just wish the same hesitancy was true in the converse. Lots of>> > Douglas Houston [mailto:douglas@davapc1.bioch.dundee.ac.uk]
> > :
> > : WHY do I need to explain why I want to do this?
> >
> > Because no one wants to give a loaded gun to
> > someone who hasn't demonstrated a good grasp of
> > gun safety.
> >
> >
> That is a very ironic response coming from someone who based on their
> sig I assume is in the US, going to someone based on their address
> appears to be in the UK... who'd a thunk it...
loaded guns, each equuipped with its own bio-bot, were recently placed in
the hands of folks who clearly lacked any grasp of gun safety, or much
else. Cheers to the UK and its top lapdog! I think a lot of Anglophones
on either side of the river have reason to feel shame.
Joseph
R. Joseph Newton Guest



Reply With Quote

