Confused. Need Help!

Discussion in 'Perl' started by Cobra Pilot, Jul 22, 2003.

  1. Cobra Pilot

    Cobra Pilot Guest

    I can't figure out why these three snippits of code are not equivalent..
    Can someone show me the correct way to do this... (Cut and paste sample code
    follows).

    @tbl is array of array refs:

    This works like I want it to:
    foreach (@tbl) {
    my $key = shift @$_;
    $hash{$key} = [@$_];
    }

    This duplicates the key:
    foreach (@tbl) {
    $hash{shift @$_} = [@$_];
    }

    And why can't I say (produces only last entry; I know this is the
    define/init syntax, isn't there an append flavor like .= ?)
    foreach (@tbl) {
    %hash = (shift @$_ => [@$_]);
    }

    --- Test program follows: ----

    #!/usr/bin/perl -w
    use strict;

    my @tbl = (
    [ "Larry Wall", "Perl Author", "555-0101" ],
    [ "Tim Bunce", "DBI Author", "555-0202" ],
    [ "Randal Schwartz", "Guy at Large", "555-0303" ],
    [ "Doug MacEachern", "Apache Man", "555-0404" ]
    );

    my %hash = ();


    # this doesn't work
    foreach (@tbl) {
    $hash{shift @$_} = [@$_];
    }

    foreach (keys %hash) {
    print $_, "=>", join " ", @{$hash{$_}}, "\n";
    }


    @tbl = (
    [ "Larry Wall", "Perl Author", "555-0101" ],
    [ "Tim Bunce", "DBI Author", "555-0202" ],
    [ "Randal Schwartz", "Guy at Large", "555-0303" ],
    [ "Doug MacEachern", "Apache Man", "555-0404" ]
    );

    %hash = ();

    # this works
    foreach (@tbl) {
    my $key = shift @$_;
    $hash{$key} = [@$_];
    }

    foreach (keys %hash) {
    print $_, "=>", join " ", @{$hash{$_}}, "\n";
    }
     
    Cobra Pilot, Jul 22, 2003
    #1
    1. Advertising

  2. Cobra Pilot

    Shawn Corey Guest

    Hi,

    First, all of the snippets modify @tbl, or rather, the arrays referenced
    in @tbl.

    Cobra Pilot wrote:

    > I can't figure out why these three snippits of code are not equivalent..
    > Can someone show me the correct way to do this... (Cut and paste sample code
    > follows).
    >
    > @tbl is array of array refs:
    >
    > This works like I want it to:
    > foreach (@tbl) {
    > my $key = shift @$_;
    > $hash{$key} = [@$_];
    > }

    The expression [@$_] can be replaced with simply $_.

    >
    > This duplicates the key:
    > foreach (@tbl) {
    > $hash{shift @$_} = [@$_];
    > }

    The array [@$_] is created first, the @$_ is shifted. This means the
    array [@$_] is not shifted. If you use $_ instead of [@$_], you get what
    you want.

    >
    > And why can't I say (produces only last entry; I know this is the
    > define/init syntax, isn't there an append flavor like .= ?)
    > foreach (@tbl) {
    > %hash = (shift @$_ => [@$_]);
    > }
    >

    Try: %hash = ( %hash, shift @$_ => $_ );
    That's the best I can think of.

    BTW, you can use Data::Dumper to view complex data structures:
    use Data::Dumper;
    print &Dumper( \%hash );
     
    Shawn Corey, Jul 22, 2003
    #2
    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. Daniel
    Replies:
    2
    Views:
    536
    Daniel
    Nov 5, 2003
  2. =?Utf-8?B?TW9l?=

    Confused: page postbacks problemn. Please help

    =?Utf-8?B?TW9l?=, Oct 19, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    415
  3. Mike Moum
    Replies:
    1
    Views:
    256
    Jp Calderone
    Jul 16, 2004
  4. shapper
    Replies:
    1
    Views:
    313
    =?Utf-8?B?Q2hldGFuIENoYXBoZWthcg==?=
    Mar 8, 2007
  5. johny.lam
    Replies:
    5
    Views:
    101
    Daniel Lucraft
    Jul 19, 2007
Loading...

Share This Page