How to assign a Hash - newbie

Discussion in 'Perl Misc' started by evillen@gmail.com, Jun 19, 2007.

  1. Guest

    Hi

    Is there a more elegant to assign one hash to another - the solution
    below works:

    my $table = 'c';
    my %dbitems;

    if($table eq 'a'){%dbitems = %a}
    elsif($table eq 'b'){%dbitems = %b}
    elsif($table eq 'c'){%dbitems = %c}
    elsif($table eq 'd'){%dbitems = %d}
    elsif($table eq 'e'){%dbitems = %e}
    elsif($table eq 'f'){%dbitems = %f}
    elsif($table eq 'g'){%dbitems = %g}

    but I hoped something like this would achieve the same:

    my $table = 'c';
    my %dbitems = %.$table;

    I've tried variations but I get syntax errors such as:
    "Can't use string ("c") as a HASH ref while "strict refs"" or "Only
    hard references are allowed by "strict refs""

    Thanks for any help
    NJH
    , Jun 19, 2007
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Hi
    >
    > Is there a more elegant to assign one hash to another - the solution
    > below works:
    >
    > my $table = 'c';
    > my %dbitems;
    >
    > if($table eq 'a'){%dbitems = %a}
    > elsif($table eq 'b'){%dbitems = %b}
    > elsif($table eq 'c'){%dbitems = %c}
    > elsif($table eq 'd'){%dbitems = %d}
    > elsif($table eq 'e'){%dbitems = %e}
    > elsif($table eq 'f'){%dbitems = %f}
    > elsif($table eq 'g'){%dbitems = %g}
    >
    > but I hoped something like this would achieve the same:
    >
    > my $table = 'c';
    > my %dbitems = %.$table;


    You need an auxiliary hash (%sel below) to do that in a sane manner.
    Untested:

    my %sel;
    @sel{ 'a' .. 'g'} = \ ( %a, %b, %c, %d, %e, %f, %g);

    my $table = 'c';
    my %dbitems = %{ $sel{ $table} };

    You may be able to work with the hashrefs from %sel directly, without
    copying the hash to %dbitems:

    my $dbitems = $sel{ $table};

    ....and then use "$dbitems->{ $key}" instead of "$dbitems{ $key}".

    Anno
    -berlin.de, Jun 19, 2007
    #2
    1. Advertising

  3. Mumia W. Guest

    On 06/19/2007 06:25 AM, wrote:
    > Hi
    >
    > Is there a more elegant to assign one hash to another - the solution
    > below works:
    >
    > my $table = 'c';
    > my %dbitems;
    >
    > if($table eq 'a'){%dbitems = %a}
    > elsif($table eq 'b'){%dbitems = %b}
    > elsif($table eq 'c'){%dbitems = %c}
    > elsif($table eq 'd'){%dbitems = %d}
    > elsif($table eq 'e'){%dbitems = %e}
    > elsif($table eq 'f'){%dbitems = %f}
    > elsif($table eq 'g'){%dbitems = %g}
    >
    > but I hoped something like this would achieve the same:
    >
    > my $table = 'c';
    > my %dbitems = %.$table;
    >
    > I've tried variations but I get syntax errors such as:
    > "Can't use string ("c") as a HASH ref while "strict refs"" or "Only
    > hard references are allowed by "strict refs""
    >
    > Thanks for any help
    > NJH
    >


    Go into a command prompt and type

    perldoc -q "variable as"

    The Perl documentation on your system should explain this.

    When you're done reading that, type "perldoc perlfaq"
    Mumia W., Jun 19, 2007
    #3
  4. mattsteel Guest

    ha scritto:

    > Hi
    >
    > Is there a more elegant to assign one hash to another - the solution
    > below works:
    >
    > my $table = 'c';
    > my %dbitems;
    >
    > if($table eq 'a'){%dbitems = %a}
    > elsif($table eq 'b'){%dbitems = %b}
    > elsif($table eq 'c'){%dbitems = %c}
    > elsif($table eq 'd'){%dbitems = %d}
    > elsif($table eq 'e'){%dbitems = %e}
    > elsif($table eq 'f'){%dbitems = %f}
    > elsif($table eq 'g'){%dbitems = %g}
    >
    > but I hoped something like this would achieve the same:
    >
    > my $table = 'c';
    > my %dbitems = %.$table;
    >
    > I've tried variations but I get syntax errors such as:
    > "Can't use string ("c") as a HASH ref while "strict refs"" or "Only
    > hard references are allowed by "strict refs""
    >
    > Thanks for any help
    > NJH


    Maybe this is what you want.

    my $table = 'c';
    my %dbitems;
    eval "\%dbitems = \%$table";

    M.
    mattsteel, Jun 19, 2007
    #4
  5. Guest

    -berlin.de wrote:
    > <> wrote in comp.lang.perl.misc:
    > > Hi
    > >
    > > Is there a more elegant to assign one hash to another - the solution
    > > below works:
    > >
    > > my $table = 'c';
    > > my %dbitems;
    > >
    > > if($table eq 'a'){%dbitems = %a}
    > > elsif($table eq 'b'){%dbitems = %b}
    > > elsif($table eq 'c'){%dbitems = %c}
    > > elsif($table eq 'd'){%dbitems = %d}
    > > elsif($table eq 'e'){%dbitems = %e}
    > > elsif($table eq 'f'){%dbitems = %f}
    > > elsif($table eq 'g'){%dbitems = %g}
    > >
    > > but I hoped something like this would achieve the same:
    > >
    > > my $table = 'c';
    > > my %dbitems = %.$table;

    >
    > You need an auxiliary hash (%sel below) to do that in a sane manner.
    > Untested:
    >
    > my %sel;
    > @sel{ 'a' .. 'g'} = \ ( %a, %b, %c, %d, %e, %f, %g);
    >
    > my $table = 'c';
    > my %dbitems = %{ $sel{ $table} };
    >
    > You may be able to work with the hashrefs from %sel directly, without
    > copying the hash to %dbitems:
    >
    > my $dbitems = $sel{ $table};
    >
    > ...and then use "$dbitems->{ $key}" instead of "$dbitems{ $key}".
    >
    > Anno



    Thanks guys, however I was advised to try using a HOH - so now I am
    trying this code:


    #START
    #!c:/perl/bin/perl.exe -w

    use strict;
    use diagnostics;

    #-------------------------------------------------
    #Description of hashes is: fieldname[label,length,category,tick,order]
    my %HoH = (
    names => {
    fore => ['Frank','Alex','Wayne'],
    sur => ['Lampard','Ferguson','Rooney'],
    },
    places => {
    north => ['Burnley', 'Chester', 'Doncaster'],
    south => ['Brighton', 'Cheltenham', 'Dover'],
    },
    );

    my $table = "names";
    my %Hash = $HoH{$table};

    print "My \%Hash is: ", %Hash;

    #END

    however this won't assign a Hash to Hash as expected. How do I assign
    the name/value pairs of "names" to %Hash?

    NJH
    , Jun 19, 2007
    #5
  6. mattsteel Guest


    >
    > my $table = "names";
    > my %Hash = $HoH{$table};
    >
    > print "My \%Hash is: ", %Hash;
    >
    > #END
    >
    > however this won't assign a Hash to Hash as expected. How do I assign
    > the name/value pairs of "names" to %Hash?
    >
    > NJH


    my %Hash = %{$HoH{$table}}
    mattsteel, Jun 19, 2007
    #6
  7. Guest

    On 19 Jun, 15:53, mattsteel <> wrote:
    > > my $table = "names";
    > > my %Hash = $HoH{$table};

    >
    > > print "My \%Hash is: ", %Hash;

    >
    > > #END

    >
    > > however this won't assign a Hash to Hash as expected. How do I assign
    > > the name/value pairs of "names" to %Hash?

    >
    > > NJH

    >
    > my %Hash = %{$HoH{$table}}


    Many thanks Matt - it's been a long day....
    , Jun 19, 2007
    #7
    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. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    612
    David A. Black
    Jul 2, 2008
  3. Thomas W.
    Replies:
    12
    Views:
    201
    Mike Moore
    Mar 4, 2011
  4. Guest1
    Replies:
    4
    Views:
    166
    Tad McClellan
    Sep 3, 2003
  5. Richard A. DeVenezia

    Using map to assign var values from hash

    Richard A. DeVenezia, Oct 12, 2003, in forum: Perl Misc
    Replies:
    9
    Views:
    124
    Richard A. DeVenezia
    Oct 14, 2003
Loading...

Share This Page