naming hash using a variable name.

Discussion in 'Perl Misc' started by ARAVIND, Jul 5, 2003.

  1. ARAVIND

    ARAVIND Guest

    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.
     
    ARAVIND, Jul 5, 2003
    #1
    1. Advertising

  2. On 5 Jul 2003 04:27:21 -0700, ARAVIND <> 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::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});

    --
    Perusion Hostmaster

    "Being against torture ought to be sort of a bipartisan thing."
    -- Karl Lehenbauer
     
    Perusion hostmaster, Jul 5, 2003
    #2
    1. Advertising

  3. Tad McClellan, Jul 5, 2003
    #3
  4. (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
     
    Eric J. Roode, Jul 5, 2003
    #4
  5. ARAVIND

    Uri Guttman Guest

    >>>>> "Ph" == Perusion hostmaster <> 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. :cool:

    >>

    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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jul 7, 2003
    #5
  6. Alan J. Flavell, Jul 7, 2003
    #6
  7. ARAVIND

    Guest

    , Jul 7, 2003
    #7
  8. ARAVIND

    Uri Guttman Guest

    >>>>> "Ph" == Perusion hostmaster <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jul 7, 2003
    #8
  9. Garry Short <> 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::Dumper;


    and find out for yourself. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 7, 2003
    #9
  10. ARAVIND

    Garry Short Guest

    Tad McClellan wrote:

    > Garry Short <> 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::Dumper;
    >
    >
    > and find out for yourself. :)
    >
    >

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

    Garry
     
    Garry Short, Jul 8, 2003
    #10
  11. Garry Short <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 12, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harman
    Replies:
    1
    Views:
    2,534
    Moiristo
    Jul 28, 2006
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,426
    Joe Smith
    Apr 22, 2007
  3. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    655
    David A. Black
    Jul 2, 2008
  5. Duncan Harris
    Replies:
    7
    Views:
    120
    Anno Siegel
    Feb 13, 2005
Loading...

Share This Page