Re: 1 $var 2 2$vars part 2
by dragonchild (Archbishop) on Feb 11, 2005 at 20:37 UTC
|
THERE ARE OTHER FACTORS WHICH I AM NOT MENTIONING THAT COME INTO PLAY.
In addition to not shouting, you're saying:
"I'm not going to give you all the information, but I'm going to get mad when you don't give me the answer I want".
Huh. We, as a community, pride ourselves at going beyond the question. Our experience is that most people asking a question are asking the wrong question. Once they learn what question they need to be asking, then that is where the majority of their growth comes from.
Now, if there is a reason why you want array => array (and I'm sure there's a good reason), you really need to state that up front. Something along the lines of:
I need to be able to take data that is in two arrays and create one array from them. Due to reasons I cannot discuss, I cannot rewrite the code to use other data structures.
At that point, no-one is going to suggest alternative data structures. Why? Because you've demonstrated that you're asking the right question. We've all been there and we know what the score is.
Relax - you're among friends.
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] |
|
|
and in response to dragonchild. if you read my post (along with my first post), i stated EXACTLY what i was looking for. the problem arises in that what i want is not generally in good practice. yes i realize that, and yes i even appreciate you pointing this out to me. but i am not looking for improvements, this is in fact what i want to do.
...and just to state one more time, i did not intend that to depict shouting and i am very appreciative of the time and help that has been bestowed upon me!
| [reply] |
Re: 1 $var 2 2$vars part 2
by friedo (Prior) on Feb 11, 2005 at 19:15 UTC
|
I reccomend using a hash of arrays instead.
my %data = ( john => [ ], mike => [ ], harry => [ ] );
foreach my $name(qw/john mike harry/) {
push @{ $data{$name} }, any_data_goes_here;
}
| [reply] [d/l] |
Re: 1 $var 2 2$vars part 2
by holli (Abbot) on Feb 11, 2005 at 19:16 UTC
|
use strict;
my @tablenums = ('1', '2', '3');
my %tables =
(
'1' => { name => "john", data => [] },
'2' => { name => "mike", data => [] },
'3' => { name => "harry", data => [] },
);
foreach (@tablenums)
{
push @{$tables{$_}->{data}}, "any", "data", "goes", "here";
}
print "$tables{1}->{name}`s data is @{$tables{1}->{data}}!";
#prints "john`s data is any data goes here!"
Update:
If you insist using dedicated arrays (which is bad style) you could use this code:
use strict;
my @tablenums = ('1', '2', '3');
my (@johnData, @mikeData, @harryData);
my %tables =
(
'1' => { name => "john", data=>\@johnData },
'2' => { name => "mike", data=>\@mikeData },
'3' => { name => "harry", data=>\@harryData },
);
foreach (@tablenums)
{
push @{$tables{$_}->{data}}, "any", "data", "goes", "here";
}
print "john`s data is @johnData!";
#prints "john`s data is any data goes here!"
see perlref and perlreftut
| [reply] [d/l] [select] |
Re: 1 $var 2 2$vars part 2
by Joost (Canon) on Feb 11, 2005 at 20:55 UTC
|
I don't really know why you have the strange data structure for %Tables, and whether you really need to have strings as the values - but with some tweaking this is the closest to your code I can get - without using eval STRING, which is usually a mistake (and expensive) to use when dealing with data structures:
my $any_data_goes_here = "bla blie bloe ";
my @tablenums = ('1', '2', '3');
my (@johnData, @mikeData, @harryData);
my %Tables = (
'1' => \@johnData,
'2' => \@mikeData,
'3' => \@harryData,
);
foreach (@tablenums){
push (@{$Tables{$_}}, $any_data_goes_here);
}
print @johnData, @mikeData, @harryData;
| [reply] [d/l] |
|
|
let me try one last time, it seems as if what some of the replies have been encompassed into my question and the wee little piggy ran all the way home and we need to bring him back...
i have an array: my @bigFatArray;
i have a scaler: my $smallskinnyScaler = "bigFat";
i want to be able to build that array and push onto it:
something like: push @{ "$smallskinnyScaler" . "Array"}, data
| [reply] |
|
|
Well, the reason everyone is wondering, is that this is really not something anyone would want to do, except in, well, exceptional circumstances, which you sort of hint at, but you never explain what the excact constraints are....
anyway:
my @data = qw(d e f);
my @bigFatArray = qw(a b c);
my $smallskinnyScaler = "bigFat";
my $ref = eval "\\\@${smallskinnyScaler}Array";
push @$ref,@data;
print @bigFatArray;
update
And please consider what would happen if $smallskinnyScaler contains "data; system('rm -f /'); #"
Have fun.
| [reply] [d/l] |
OT: Posting Etiquette
by K_M_McMahon (Hermit) on Feb 11, 2005 at 20:32 UTC
|
Update: Thanks for the tip JDporter. Title corrected.
Please go back and read the Writeup formatting tips section and the section on Modifying your original post. You shouldn't modify your original post without specifically noting what changes you have made.
Also, there is no reason to shout in the monastery. We try to be as helpful as we can be based on the information we have been given.
| [reply] |
|
|
--
jdporter
| [reply] |
Re: 1 $var 2 2$vars part 2
by jls13 (Initiate) on Feb 11, 2005 at 20:37 UTC
|
i am not shouting, i apologize if you took it that way. i just wanted to ensure that was read. as i said a couple of times, i greatly appreciate your help. | [reply] |
Re: 1 $var 2 2$vars part 2
by holli (Abbot) on Feb 11, 2005 at 21:29 UTC
|
use strict;
use warnings;
my @tablenums = ('1', '2', '3');
my (@johnData, @mikeData, @harryData);
my @any = ("any", "data", "goes", "here");
my %Tables = (
'1' => "john",
'2' => "mike",
'3' => "harry",
);
foreach (@tablenums){
eval "push \@${Tables{$_}}Data, (\"" . join ('","', @any, $_) . "\");"
+;
}
print "john`s data is @johnData!";
#prints "john`s data is any data goes here 1!"
I think you should read the documentation of eval() again.
| [reply] [d/l] [select] |
Re: 1 $var 2 2$vars part 2
by merlyn (Sage) on Feb 12, 2005 at 00:51 UTC
|
If part of your solution demands "eval string", I suggest you back up a step and solve the problem that lead you to needing this part of the solution.
Your refusal to elaborate on context is going to only aggravate anyone who is willing to help. Why are you being secretive?
| [reply] |
Re: 1 $var 2 2$vars part 2
by Arunbear (Prior) on Feb 11, 2005 at 22:35 UTC
|
If you don't mind letting your arrays have package scope, you can use soft references like this:
use strict;
use warnings;
use Data::Dumper;
my @tablenums = ('1', '2', '3');
our (@johnData, @mikeData, @harryData);
my @any = ("any", "data", "goes", "here");
my %Tables = (
'1' => "john",
'2' => "mike",
'3' => "harry",
);
foreach (@tablenums){
no strict 'refs';
push @{"$Tables{$_}Data"}, @any;
}
print Dumper(\(@johnData, @mikeData, @harryData));
Further reading: Symbolic references
I do not know what I may appear to the world; but to myself I seem to have been only like a boy playing on the seashore, and diverting myself in now and then finding a smoother pebble or a prettier shell than ordinary, whilst the great ocean of truth lay all undiscovered before me.
-- Isaac Newton
| [reply] [d/l] |