Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Confused. Need Help!

Reply
Thread Tools

Confused. Need Help!

 
 
Cobra Pilot
Guest
Posts: n/a
 
      07-22-2003
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";
}






 
Reply With Quote
 
 
 
 
Shawn Corey
Guest
Posts: n/a
 
      07-22-2003
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:umper to view complex data structures:
use Data:umper;
print &Dumper( \%hash );


 
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
Need to "shred" hardive..need software. Xeno Chauvin Computer Support 4 03-13-2005 01:26 PM
Need help with need to Force Garbage Collect John Java 1 08-19-2004 03:32 PM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM
Need to compare Photos to DVD software - Need Help? Bob Digital Photography 4 02-04-2004 02:13 AM



Advertisments