Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Very newbie Q anout $,@ and %

Reply
Thread Tools

Very newbie Q anout $,@ and %

 
 
Torch
Guest
Posts: n/a
 
      11-12-2003
I know that
$ is used to declare variables
@ is used to declare arrays
% is used to declare ???????
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-12-2003
In article <(E-Mail Removed)> , Torch wrote:
> 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).

--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-12-2003
Torch wrote:
> 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


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-12-2003

"Jürgen Exner" <(E-Mail Removed)> wrote:
> 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

--
For the last month, a large number of PSNs in the Arpa[Inter-]net have been
reporting symptoms of congestion ... These reports have been accompanied by an
increasing number of user complaints ... As of June,... the Arpanet contained
47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] * http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-12-2003
Jürgen Exner <(E-Mail Removed)> wrote:
> Torch wrote:
>> 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.



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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Josef Möllers
Guest
Posts: n/a
 
      11-13-2003
Ben Morrow wrote:
>
> "Jürgen Exner" <(E-Mail Removed)> wrote:
> > You can think of hashes in two ways. They are like arrays, except that they
> > allow arbitrary scalar values as indices.

>
> 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.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-13-2003

Josef =?iso-8859-1?Q?M=F6llers?= <(E-Mail Removed)> wrote:
> Ben Morrow wrote:
> >
> > "Jürgen Exner" <(E-Mail Removed)> wrote:
> > > You can think of hashes in two ways. They are like arrays,
> > > except that they allow arbitrary scalar values as indices.

> >
> > Arbitrary *string* values.

>
> 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

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces molit animos, tristesque mentes erigit. | (E-Mail Removed)
Musica vel ipsas arbores et horridas movet feras. |
 
Reply With Quote
 
Josef Möllers
Guest
Posts: n/a
 
      11-13-2003
Ben Morrow wrote:

> works. Now try
>
> use strict;


Point taken.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
 
Reply With Quote
 
the zorg
Guest
Posts: n/a
 
      11-13-2003
On Thu, 13 Nov 2003 13:23:08 +0100, Josef =?iso-8859-1?Q?M=F6llers?=
<(E-Mail Removed)> wrote:

>Ben Morrow wrote:
>
>> works. Now try
>> =

>
>> use strict;

>
>Point taken.
>


[ ] You know Perl
[X] You don't know Perl.

Try to read....

etc

Z

>-- =
>
>Josef M=F6llers (Pinguinpfleger bei FSC)
> If failure had no penalty success would not be a prize
> -- T. Pratchett


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error in app log file anout aspnet_perf.dll =?Utf-8?B?ZGF2aWQ=?= ASP .Net 6 11-15-2007 09:14 PM
very very very long integer shanx__=|;- C Programming 19 10-19-2004 03:55 PM
very very very long integer Abhishek Jha C Programming 4 10-17-2004 08:19 AM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments