@data = @items{@fields} construct after a foreach - where can I find a literature reference to this?
From: Henry Lenzi (strangeuser_at_strangeuser.com)
Date: 02/25/05
- Next message: GreenLeaf: "Re: Read from keyboard"
- Previous message: Doug O'Leary: "Thanks Uri Guttman (complex data structures)"
- Next in thread: Brian McCauley: "Re: @data = @items{@fields} construct after a foreach - where can I find a literature reference to this?"
- Reply: Brian McCauley: "Re: @data = @items{@fields} construct after a foreach - where can I find a literature reference to this?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 25 Feb 2005 01:02:00 -0500
# Hi all --
# I'm unable to find a justification, in the Perl literature, of the @data=@items{@fields}
# construct. In fact, I can't find it!
# In a previous message, Message-Id: <slrnd1qnn6.7fp.tadmc@magna.augustmail.com>
# the following script sprung from the discussion:
#!/usr/bin/perl
use warnings;
use strict;
my @array;
_writeMiniCard();
sub _writeMiniCard
{
print "Choice (1) (2) \n";
my $choice = <STDIN>;
if ($choice == 1) {
_enterCard(); }
elsif ($choice == 2) {
print "choice 2\n";#stub
}}
# Thanks to Tad McClellan for this
sub _enterCard {
my %items;
my @fields = ( 'AUTHOR(S)= ', 'TITLE= ', 'KEYWORDS= ',
'SOURCE/JOURNAL= ', 'VOL= ', 'YEAR= ',
'PAGES= ', 'EDITOR= ', 'ETC= ', 'X-REF= '
);
foreach my $prompt ( @fields ) {
print "$prompt ";
$items{$prompt} = $prompt . <STDIN>; # Aah! I Got it!
}
# What came next I found truly intriguing!
# Wow! It will iterate ?! It blew my mind!
# How come this happened ?!?!
my @array = @items{ @fields };
print "\n @array\n";
}
# Here's my tentative explanation (*please* correct me if I'm wrong):
# 1) The foreach expression offers a list context. The control variable
# ('$prompt', in this case) receives each element of the @fields array,
# and through '$items{$prompt} = $prompt . <STDIN>' the lexically scoped
# hash '%items' is filled. That is, the result is in list context;
# 2) Then, 'my @array = @items{ @fields }'...What is going on is an
# attribution operation, and pairs abtained from '@items { @fields }'
# fill up the '@array'.
# I *think* I might have justified the functioning of the code.
# However, I would be just *so* grateful if anyone could point
# out a source in the Perl "literature" that specifically mentions
# or justifies the iteration form '@array = @items{ @fields }'.
# Thanks very much,
# best regards,
# Henry Lenzi
- Next message: GreenLeaf: "Re: Read from keyboard"
- Previous message: Doug O'Leary: "Thanks Uri Guttman (complex data structures)"
- Next in thread: Brian McCauley: "Re: @data = @items{@fields} construct after a foreach - where can I find a literature reference to this?"
- Reply: Brian McCauley: "Re: @data = @items{@fields} construct after a foreach - where can I find a literature reference to this?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]