Very newbie Q anout $,@ and %

Discussion in 'Perl Misc' started by Torch, Nov 12, 2003.

  1. Torch

    Torch Guest

    I know that
    $ is used to declare variables
    @ is used to declare arrays
    % is used to declare ???????
     
    Torch, Nov 12, 2003
    #1
    1. Advertising

  2. In article <>, 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
     
    Andreas Kahari, Nov 12, 2003
    #2
    1. Advertising

  3. 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
     
    Jürgen Exner, Nov 12, 2003
    #3
  4. Torch

    Ben Morrow Guest

    "Jürgen Exner" <> 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] *
     
    Ben Morrow, Nov 12, 2003
    #4
  5. Jürgen Exner <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 12, 2003
    #5
  6. Ben Morrow wrote:
    >
    > "Jürgen Exner" <> 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
     
    Josef Möllers, Nov 13, 2003
    #6
  7. Torch

    Ben Morrow Guest

    Josef =?iso-8859-1?Q?M=F6llers?= <> wrote:
    > Ben Morrow wrote:
    > >
    > > "Jürgen Exner" <> 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. |
    Musica vel ipsas arbores et horridas movet feras. |
     
    Ben Morrow, Nov 13, 2003
    #7
  8. 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
     
    Josef Möllers, Nov 13, 2003
    #8
  9. Torch

    the zorg Guest

    On Thu, 13 Nov 2003 13:23:08 +0100, Josef =?iso-8859-1?Q?M=F6llers?=
    <> 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
     
    the zorg, Nov 13, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    518
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,719
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    468
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    542
    Dave Thompson
    Feb 14, 2005
  5. =?Utf-8?B?ZGF2aWQ=?=

    error in app log file anout aspnet_perf.dll

    =?Utf-8?B?ZGF2aWQ=?=, Nov 13, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,371
    =?Utf-8?B?ZGF2aWQ=?=
    Nov 15, 2007
Loading...

Share This Page