Initializing an anonymous hash (repost)

Discussion in 'Perl Misc' started by Neil Shadrach, Apr 8, 2004.

  1. If I have 2 arrays @k and @v I can initialize a hash %h with
    @h{@k}=@v;
    If my next step is
    push @a,\%h;
    is there a neat way to skip creating %h and initialize an anonymous
    hash for use in a line like
    push @a, {};

    ( Apologies for reposting via Google but the original doesn't seem to
    have made it off the in-house server )
     
    Neil Shadrach, Apr 8, 2004
    #1
    1. Advertising

  2. (Neil Shadrach) writes:

    > If I have 2 arrays @k and @v I can initialize a hash %h with
    > @h{@k}=@v;
    > If my next step is
    > push @a,\%h;
    > is there a neat way to skip creating %h and initialize an anonymous
    > hash for use in a line like
    > push @a, {};


    No. You can rearange the problem in various ways with do{} or map()
    but there is nothing neat builting. If you just want neat (not fast)
    you could, or course write a function:

    sub anon_slice(\@\@) {
    my %h;
    my $keys = shift;
    @h{@$keys} = @{shift()};
    \%h;
    }

    push @a => anon_slice( @h => @v );

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Apr 8, 2004
    #2
    1. Advertising

  3. Neil Shadrach <> wrote:
    > If I have 2 arrays @k and @v I can initialize a hash %h with
    > @h{@k}=@v;
    > If my next step is
    > push @a,\%h;
    > is there a neat way to skip creating %h and initialize an anonymous
    > hash for use in a line like
    > push @a, {};



    push @a, { map { $k[$_], $v[$_] } 0 .. $#k };


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 8, 2004
    #3
  4. Neil Shadrach

    Uri Guttman Guest

    >>>>> "NS" == Neil Shadrach <> writes:

    NS> If I have 2 arrays @k and @v I can initialize a hash %h with
    NS> @h{@k}=@v;
    NS> If my next step is
    NS> push @a,\%h;
    NS> is there a neat way to skip creating %h and initialize an anonymous
    NS> hash for use in a line like
    NS> push @a, {};

    besides the other two answers, i have done this. it works if you can
    allow destruction of one of the arrays:

    push( @a, { map { shift @k, $_ } @v } ) ;

    you can destroy @v instead of @k:

    push( @a, { map { $_, shift @v } @k } ) ;

    either way should be faster then the others but benchmark to be sure.

    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, Apr 8, 2004
    #4
  5. Neil Shadrach

    Guest

    (Neil Shadrach) wrote:
    > If I have 2 arrays @k and @v I can initialize a hash %h with
    > @h{@k}=@v;
    > If my next step is
    > push @a,\%h;
    > is there a neat way to skip creating %h and initialize an anonymous
    > hash for use in a line like
    > push @a, {};


    Autovivify the hash in place.

    use Data::Dumper;
    my @a=({},{});
    my @k=1..6;
    my @v = map $_*$_, 1..6;
    warn "Ugly, but works";
    @{$a[@a]}{@k}=@v;
    print Dumper(\@a);
    __END__
    Ugly, but works at - line 5.
    $VAR1 = [
    {},
    {},
    {
    '6' => 36,
    '4' => 16,
    '1' => 1,
    '3' => 9,
    '2' => 4,
    '5' => 25
    }
    ];


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 9, 2004
    #5
  6. Neil Shadrach

    Brad Baxter Guest

    On Tue, 13 Apr 2004, Neil Shadrach wrote:

    > wrote in message
    >
    > > warn "Ugly, but works";

    >
    > You're being too modest.
    >
    > > @{$a[@a]}{@k}=@v;

    >
    > If I rework my original problem to be a little closer to your solution
    > :)
    >
    > I wanted to eliminate %h from
    > @h{@k}=@v; $a[@a]=\%h;
    >
    > I can just about convince myself that I understand if I allow the
    > pseudo-step of dereferencing both sides of the second statement as
    > arrays and then substitute in the first
    > @{$a[@a]}=@h; # ok I know I couldn't actually use @h like that - I did
    > say 'just about'
    >
    > I'm not going to offer to stand up and jsutify that at a perl
    > conference though.


    I'm not sure that describes it well. I'm not sure this does either. :)
    But ...

    o scalar @a is always just beyond the end of the array, so
    o assigning to $a[@a] is essentially the same as push @a, ...
    o you said, "If my next step is push @a,\%h;", so
    o @{$a[@a]}{@k}=@v is autovivifying an anonymous hash at element $a[@a]
    just as @h{@k}=@v would autovivify %h if it didn't exist before (and
    strictures didn't block it).

    Regards,

    Brad
     
    Brad Baxter, Apr 14, 2004
    #6
    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. mark fine
    Replies:
    1
    Views:
    479
    Michael Jørgensen
    Nov 9, 2004
  2. Reporter
    Replies:
    3
    Views:
    489
    Mike Schilling
    May 12, 2007
  3. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  4. Replies:
    1
    Views:
    234
  5. Justin C
    Replies:
    1
    Views:
    198
    Justin C
    Oct 7, 2013
Loading...

Share This Page