473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Confused. Need Help!

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";
}


Jul 19 '05 #1
1 2083
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 );
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Suzette | last post by:
I'm going through Dan Appleman's book on API and am getting so confused. Can anyone suggest additional resources that might help me get the fog in my brain cleared? Thanks Sue
3
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading...
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
0
by: Chris L | last post by:
Greetings, I am confused as to just how something works in the Northwinds sample. When you add a qty of items ordered to an "Order", the Field labeled "Units On Order" of the "Products" form...
10
by: Lauren Wilson | last post by:
Ok I have searched the MS website for info on this. I am totally confused. If I want to deploy an Access 2003 app and allow my users to run it using Access 2003 Runtime, where do I get the...
6
by: m_a_t_t | last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on chapter 1.5.1 and here's the program you're sposed to make: #include <stdio.h> /* copy input to output; 1st version */...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
9
by: JohnSmith | last post by:
I suspect this is easy, but I have been stumped for a day trying to solve this.. I want to be able to have an unlimited number of aspx pages that all use the code in one class file. I want code...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
2
by: abshirf2 | last post by:
Hello all, I am really confused, please help! :( I have tomcat 5.5 installed on my machine and i have the JDK/JRE installed. Everything is working fine. Now what i want to do is install php...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.