Chas. Owens wrote:
>
On Jan 22, 2008 8:45 PM, bootleg86 bootleg86 <[EMAIL PROTECTED]>
wrote:
I came across this construct
foreach $i ( @{$y} ) {
#do something
}
Is @ referring to some default array that doesn't need to be declared?
Also it's using the associative version of an array?
I always thought only hashes were associative.
snip
If the code works (and it isn't guarenteed to)
I'm not sure what you mean here. It's as guaranteed to work as any other
Perl code snippet I've seen.
then $y holds a reference to an array and @{} is being used to
corerce it into behaving like an array (i.e. dereferencing it).
It's not coercing $y to do anything. It's accessing the array to which
$y refers.
Rob
If you 'call' @{$y} you are using the array with the content of $y as
name of the array.
my $y=foo
Sayin @{$y} is the same as @foo. So if you haven't any defined array
called foo, when you go into the foreach. It does nothing because there
is nothing to do. Yu don't have any error report because the syntax is
okay, everything is okay unless you miss the array you want to work
with doesn't exist.
######this piece of code does nothing, because the is nothing to work with
#!/usr/bin/perl
my $y="foo";
foreach (@{$y}){
print "i'm doing something with $_ \n ";
}
##### in this one we have the array.
#!/usr/bin/perl
@foo=qw/ foo1 foo2 foo2 /;
my $y="foo";
foreach (@{$y}){
print "i'm doing something with $_ \n ";
}
the output:
[EMAIL PROTECTED]:~$ perl test.pl
i'm doing something with foo1
i'm doing something with foo2
i'm doing something with foo2
Does this explanation solve your question?
Sorry if i have mistakes in my english.
Best regards
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/