Very newbie Q anout $,@ and %

T

Torch

I know that
$ is used to declare variables
@ is used to declare arrays
% is used to declare ???????
 
A

Andreas Kahari

I know that
$ is used to declare variables
@ is used to declare arrays
% is used to declare ???????

% is used to denote a hash (associative array).
 
J

Jürgen Exner

Torch said:
I know that
$ is used to declare variables
To be more precise: scalar variable
@ is used to declare arrays
To be more precise: array variable
% is used to declare ???????
Hash variables.

Actually, those characters don't declare variables but they indicate the
type of a variable, in a declarations as well as in any application of the
variable. They are an integral part of the variable name itself.

You can think of hashes in two ways. They are like arrays, except that they
allow arbitrary scalar values as indices. And they are like a
one-dimensional function or mapping, because they map scalars to scalars.
Both views are fine, sometimes the one is more convenient, sometimes the
other.

jue
 
B

Ben Morrow

Jürgen Exner said:
You can think of hashes in two ways. They are like arrays, except that they
allow arbitrary scalar values as indices.

Arbitrary *string* values.

Ben
 
T

Tad McClellan

Jürgen Exner said:
To be more precise: scalar variable

To be more precise: array variable

Hash variables.

Actually, those characters don't declare variables but they indicate the
type of a variable, in a declarations as well as in any application of the
variable. They are an integral part of the variable name itself.


And the technical term for those characters is "sigil".
 
J

Josef Möllers

Ben said:
Arbitrary *string* values.

Says who?

The camel book says: "... a scalar always contains a single value. This
value may be a number, a string, or a reference to another piece of
data. Or, there might even be no value at all, in which case the scalar
is said to be undefined."
But, although it also says "As we said earlier, a hash is just a funny
kind of array in which you look values up using key strings instead of
numbers.", let's give it a try:

my %hash = ();
$hash{0x10} = "Sixteen";
print $hash{16}, "\n";

-> Sixteen

And even

my %hash = ();
$hash{undef} = "Not defined";
print $hash{undef}, "\n";

-> Not defined

and ...

$a = 1;
$b = \$a;
my %hash = ();
$hash{$b} = "Reference to a";
print $hash{\$a}, "\n";

-> Reference to a

So, in all aspects, "scalar" is perfectly ok.
 
B

Ben Morrow

Josef =?iso-8859-1?Q?M=F6llers?= said:
Says who?

Says the perl source, apart from anything else... :)
The camel book says: "... a scalar always contains a single value. This
value may be a number, a string, or a reference to another piece of
data. Or, there might even be no value at all, in which case the scalar
is said to be undefined."
Yes.

But, although it also says "As we said earlier, a hash is just a funny
kind of array in which you look values up using key strings instead of
numbers.", let's give it a try:

my %hash = ();
$hash{0x10} = "Sixteen";
print $hash{16}, "\n";

-> Sixteen

Yes. The hash key is stringified, and both 16 and 0x10 stringify to
"16".
my %hash = ();
$hash{undef} = "Not defined";
print $hash{undef}, "\n";

-> Not defined

print keys %hash

-> undef

A bareword in the {} of a hash element is stringified.
$a = 1;
$b = \$a;
my %hash = ();
$hash{$b} = "Reference to a";
print $hash{\$a}, "\n";

-> Reference to a

Again, \$a stringifies to something like "SCALAR(0x8168544)", so this
works. Now try

use strict;

my $x = 1;
my %y;
$y{\$x} = 2;
my $z = (keys %y)[0];
print $$z;

-> Can't use string ("SCALAR(0x8154b70)") as a SCALAR ref while "strict
refs" in use
So, in all aspects, "scalar" is perfectly ok.

I think not :)

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top