On Saturday, May 31, 2003, at 01:47 PM, Richard Heintze wrote:

I tried "use strict;" and that worked. Are you
encouraging me to use "use warn;" too? That does not
work.

use warnings;


It will alert you to potential problems in your code, while strict makes sure you obey the good programmer rules.

What is the name for this syntax: "(keys %{$x})"? Are
we dereferencing and casting? Why do I use "%" here
but when I want to access a specific element, you say
to use the syntax "${$y}{$i}". The index operator {}
needs to work on the entire hash, not a a scalar! By
using a $, we indexing into a scalar, no?

Casting? Isn't that one of those ugly C words? <laughs> Generally, in Perl, data is simply what we need it to be, no casting required.


Dereferencing, yes. Let's see if we can clear some of this up.

# Simple Variables
$scalar # a single value

@array  # an array of scalars
$array[$index]  # the single scalar in @array at position $index

%hash   # a hash of scalars
$hash{$key}     # the single scalar in %hash referenced by the string $key

# References
$scalar_ref = \$scalar # a reference to a scalar, which also happens to be a scalar
$array_ref = [EMAIL PROTECTED] # a reference to an array, still a scalar
$hash_ref = \%hash # a reference to an hash, still a scalar


# Dereferencing
$$scalar_ref    # or ${$scalar_ref}, but the braces aren't needed here
# the scalar value referenced by $scalar_ref

$$array_ref[$index]     # or ${$array_ref}[$index] or $array_ref->[$index]
# the scalar value in the array referenced by $array_ref at $index

$$hash_ref{$key}        # or ${$hash_ref}{$key} or $hash_ref->{$key}
# the scalar value in the hash referenced by $hash_ref by $key

Hopefully that clears things up, at least a little. References are mostly useful when building complex data structures, since they are just scalars and therefore can be stored inside arrays and hashes. In simple code like what you've show us though, stick to the simple structures and make things easier on yourself.

James


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



Reply via email to