Richard Heintze wrote:
> 
> Some help understanding this program would be greatly
> appreciated! I'm really struggling with this perl
> language!

perldoc perldata
perldoc perlreftut
perldoc perlref
perldoc perldsc
perldoc perllol


> my $x= {'d' => 'y', 'f' => 'g'},
>     $y = ['a', 'b', 'c', 'd'];
> # This works! Good!

$x is now a lexical scalar which contains an anonymous hash and $y is
now a package scalar which contains an anonymous array.  If you want
both variables to be lexical you will have to declare them differently.

my $x = {'d' => 'y', 'f' => 'g'};
my $y = ['a', 'b', 'c', 'd'];

Or:

my ( $x, $y ) = ( {'d' => 'y', 'f' => 'g'}, ['a', 'b', 'c', 'd'] );


> foreach my $i (@{$y}){ print "array i = $i\n" }
> 
> # (1) Why does not this work? How do I index an array?

What do you mean by "not work"?  It looks like it works to me.


> # (2) How do I compute the length of y instead of hard
> coding 3?

An array in scalar context returns the number of elements in the array.


> for(my $i = 0; $i <= 3; $i++) {
>   print "y[$i] = "[EMAIL PROTECTED]"\n";
> }

You could also do that like this:

for my $i ( 0 .. $#$y ) {
  print "y[$i] = $y->[$i]\n";
}


> # $i receives the proper values
> foreach my $i (keys %{$x}) {
>   # (4) Why does not this work? How do I index into my
> hash?
>   print "hash i = $i => ".$x{$i}."\n";
> }

for my $i ( keys %$x ) {
  print "hash i = $i => $x->{$i}\n";
}


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to