naming hash using a variable name.

A

ARAVIND

I have a variable output from one part of program,
$tmp1 = I.LUV.U;
Now,
I want to create a variable of type hash with a name I.LUV.U
i.e. the name of hash to be same as $tmp1,

how can i achieve this?

Regards,
Aravind.
 
P

Perusion hostmaster

I have a variable output from one part of program,
$tmp1 = I.LUV.U;
Now,
I want to create a variable of type hash with a name I.LUV.U
i.e. the name of hash to be same as $tmp1,

Easy -- use references.

use strict;
use Data::Dumper;
$Data::Dumper::Terse = 1;

my @rand = qw/foo bar baz/;

my $tmp1 = $rand[int rand(scalar(@rand))];

my %hash;

$hash{$tmp1} = {};

if(! @ARGV) {
@ARGV = (
'test value 1, goes to index 0',
'test value 2, goes to index 1',
'test value 3, goes to index 2',
);
}

for(my $i = 0; $i < @ARGV; $i++) {
$hash{$tmp1}{$ARGV[$i]} = $i;
}

print Dumper(\%hash);


If you insist on the tired Perl 4 way:

use strict;
use Data::Dumper;
$Data::Dumper::Terse = 1;

my @rand = qw/foo bar baz/;

my $tmp1 = $rand[int rand(scalar(@rand))];


if(! @ARGV) {
@ARGV = (
'test value 1, goes to index 0',
'test value 2, goes to index 1',
'test value 3, goes to index 2',
);
}

for(my $i = 0; $i < @ARGV; $i++) {
no strict;
${$tmp1}{$ARGV[$i]} = $i;
}

no strict;
print "$tmp1=";
print Dumper(\%{$tmp1});
 
E

Eric J. Roode

(e-mail address removed) (ARAVIND) wrote in @posting.google.com:
I have a variable output from one part of program,
$tmp1 = I.LUV.U;
Now,
I want to create a variable of type hash with a name I.LUV.U
i.e. the name of hash to be same as $tmp1,

how can i achieve this?

The correct answer to this question is "Why do you think you need to do
this?"

It can be done, but it's generally a bad idea. Better to use a nested
hash.
 
U

Uri Guttman

Ph" == Perusion hostmaster said:
my $tmp1 = $rand[rand @rand];

that is the classic select a random element from an array idiom.

Ph> With luck I will remember that next time and flag it as
Ph> authoritative. :cool:

Ph> I do know that. I am not a big fan of autovivification, and almost
Ph> always explicitly instantiate anonymous refs. In fact, I would
Ph> like to see:

Ph> use strict qw/no_autovivify/;

autoviv is good for you in general. if you had to manually do it each
time, it would be a major pain to dynamically generate deep
structures. you couldn't just push into a hash slot, you would have to
check and then initialize it each time:

push( @{$self->{'foo'}}, $stuff ) ;
push( @{$self->{'foo'} ||= [] }, $stuff ) ;

and that gets worse with each level.

Ph> for(my $i = 0; $i < @ARGV; $i++) { $hash{$tmp1}{$ARGV[$i]} = $i;
Ph> Done that way to try and talk to the less-experienced on their
Ph> level.

and you can even talk lower level that your code. you have to write code
to some level of skill and not below that. i prefer to use hash slices
as they are not complex, obscure and are teachable. look at my paper on
them at

http://www.sysarch.com/perl/tutorials/hash-slices.txt


Ph> If you insist on the tired Perl 4 way:
Ph> You are probably right.

s/probably// ;

:)

uri
 
A

arasu

Hi,

I read all the three pages, the problem is solved by namespacing
which is what we are trying to do with a hash.

how about this -

package A;
{
...
}

my $var = "/";

my $symref = "A::".$var;

$$symref = "fafaf";

regards,
arasu
 
U

Uri Guttman

Ph> I have used it where appropriate. In general with the type of
Ph> simple data that my rather pedestrian application uses, it is not
Ph> needed. AFAIK, explicit instantiation is not a performance
Ph> penalty, and it helps the code better document itself.

but it gets noisy. and i can (but won't) show you cases where it gets
very annoying. and with multiple levels being accessed in one expression
it becomes massively annoying. having done that very thing in perl4
where i had to write all the create and access code for a massive and
deep structure, i appreciate autoviv a great deal. now it can bite you
in subtle ways but they are much less a problem than not having it at
all. note that perl6 will still have it but exists will not do it which
is a good thing.

uri
 
T

Tad McClellan

Garry Short said:
what does :
@aliases = @{ $expand_aliases{ $alias } } ;
do?


Dereferences the array ref contained in $expand_aliases{ $alias }.

It is an application of "Use Rule 1" from:

perldoc perlreftut

I'm assuming that: [snip]
would give the same results as: [snip]
Is that right?


use Data::Dumper;


and find out for yourself. :)
 
G

Garry Short

Tad said:
Garry Short said:
what does :
@aliases = @{ $expand_aliases{ $alias } } ;
do?


Dereferences the array ref contained in $expand_aliases{ $alias }.

It is an application of "Use Rule 1" from:

perldoc perlreftut

I'm assuming that: [snip]
would give the same results as: [snip]
Is that right?


use Data::Dumper;


and find out for yourself. :)
Thanks, I was right! That Data::Dumper could be handy - I'll have to
remember that one!

Garry
 
T

Tad McClellan

Garry Short said:
That Data::Dumper could be handy - I'll have to
remember that one!


That is why it is mentioned in the Perl FAQ. :)

You should (eventually) read each and every FAQ, there is bound
to be lots of similarly time-saving thingies that you are as yet
unaware of.

The FAQ is ineffective when you do not read it. :) :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top