Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > naming hash using a variable name.

Reply
Thread Tools

naming hash using a variable name.

 
 
ARAVIND
Guest
Posts: n/a
 
      07-05-2003
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.
 
Reply With Quote
 
 
 
 
Perusion hostmaster
Guest
Posts: n/a
 
      07-05-2003
On 5 Jul 2003 04:27:21 -0700, ARAVIND <(E-Mail Removed)> wrote:
> 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:umper;
$Data:umper::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:umper;
$Data:umper::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});

--
Perusion Hostmaster

"Being against torture ought to be sort of a bipartisan thing."
-- Karl Lehenbauer
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      07-05-2003
ARAVIND <(E-Mail Removed)> wrote:

> i.e. the name of hash to be same as $tmp1,
>
> how can i achieve this?



Friends don't let friends use symbolic references.

Use a hash and real references instead:

http://www.plover.com/~mjd/perl/varvarname.html
http://www.plover.com/~mjd/perl/varvarname2.html
http://www.plover.com/~mjd/perl/varvarname3.html


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      07-05-2003
(E-Mail Removed) (ARAVIND) wrote in news:1d21ceeb.0307050327.3e140fd1
@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.

--
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      07-07-2003
>>>>> "Ph" == Perusion hostmaster <(E-Mail Removed)> writes:

>> 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.

>>

Ph> $hash{$tmp1} = {};
>> no need to initialize that. autovivification will handle it for
>> you.


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;
>>
>> @{$hash{$tmp1}}{@ARGV} = 0 .. $#ARGV ;
>>


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:
>> no, he doesn't insist upon it. you shouldn't even show how symrefs
>> can be done. go with your correct instincts and not show it at all.


Ph> You are probably right.

s/probably// ;



uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      07-07-2003
On Mon, Jul 7, Uri Guttman inscribed on the eternal scroll:

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


404 not found!

Google found it at
http://www.sysarch.com/perl/tutorials/hash_slices.txt

(underscore instead of minus)

 
Reply With Quote
 
arasu@nospam.org
Guest
Posts: n/a
 
      07-07-2003
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


>
> http://www.plover.com/~mjd/perl/varvarname.html
> http://www.plover.com/~mjd/perl/varvarname2.html
> http://www.plover.com/~mjd/perl/varvarname3.html

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      07-07-2003
>>>>> "Ph" == Perusion hostmaster <(E-Mail Removed)> writes:


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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-07-2003
Garry Short <(E-Mail Removed)> wrote:

> 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:umper;


and find out for yourself.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Garry Short
Guest
Posts: n/a
 
      07-08-2003
Tad McClellan wrote:

> Garry Short <(E-Mail Removed)> wrote:
>
>> 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:umper;
>
>
> and find out for yourself.
>
>

Thanks, I was right! That Data:umper could be handy - I'll have to
remember that one!

Garry


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Javax.naming Exception: name not found in naming service. Harman Java 1 07-28-2006 08:51 AM
naming a variable with the datum from another variable Duncan Harris Perl Misc 7 02-13-2005 06:48 PM



Advertisments