Should the element contains in an array/hash be scales?

Discussion in 'Perl Misc' started by Jking, Aug 24, 2008.

  1. Jking

    Jking Guest

    hi, guys,

    Should the element contains in an array/hash be a scale, so that I can only
    put references pointing to arrays/hashes in them?

    In other words, is the assignment legal?

    @a=(0,1);
    %hi=( "hi"=>@a );

    Thank you.

    Justin
    08/24
     
    Jking, Aug 24, 2008
    #1
    1. Advertising

  2. Jking wrote:
    > Should the element contains in an array/hash be a scale, so that I can only
    > put references pointing to arrays/hashes in them?


    Yes. Array elements and hash values must be a scalar (not "scale").
    References are scalars, so they can be stored in an array or hash.

    > In other words, is the assignment legal?
    >
    > @a=(0,1);
    > %hi=( "hi"=>@a );


    That depends on what you mean by "legal." The syntax itself is valid.
    (You can initialize a hash with an array.) It probably doesn't do what
    you want, though. To perl, this looks like

    %h = (hi => 0, 1)

    Hash initializations should have an even number of elements (i.e. pairs
    of keys & values). If you run this with warnings enabled you'll get

    Odd number of elements in hash assignment...

    because perl sees the key 'hi' with a value of 0 and the key '1' with no
    corresponding value. What you (presumably) want is this:

    %h = (hi => \@a);

    perldoc perlreftut for a crash course in using references.

    -mjc
     
    Michael Carman, Aug 24, 2008
    #2
    1. Advertising

  3. Jking

    Ben Morrow Guest

    Quoth "Jking" <>:
    >
    > Should the element contains in an array/hash be a scale, so that I can only
    > put references pointing to arrays/hashes in them?


    I presume you mean 'scalar'? Yes, that is correct: arrays and hashes
    only contain scalars, so multi-level structures have to be built using
    (explicit) references, unlike some other languages where the references
    are implicit.

    > In other words, is the assignment legal?
    >
    > @a=(0,1);
    > %hi=( "hi"=>@a );


    It is, but it may not mean what you think. It is equivalent to

    %hi = ("hi" => 0, 1);

    which is equivalent to

    %hi = ("hi" => 0, 1 => undef);

    since hash assignment requires an even-sized list. It will create two
    hash elements, "hi" with a value of 0 and "1" with a value of undef. If
    you want a single, array-(reference-)valued element, you need

    %hi = ("hi" => \@a);

    See perldoc perlreftut and perldoc perldsc.

    Ben

    --
    Although few may originate a policy, we are all able to judge it.
    Pericles of Athens, c.430 B.C.
     
    Ben Morrow, Aug 24, 2008
    #3
  4. "Jking" <> wrote:
    >Should the element contains in an array/hash be a scale, so that I can only


    I suppose you mean scalar? A scale is something that fish have on their
    skin.

    >put references pointing to arrays/hashes in them?


    Yes.

    >In other words, is the assignment legal?
    >
    >@a=(0,1);
    >%hi=( "hi"=>@a );


    Yes, it is legitimate, but it probably doesn't do what you expect it to
    do.

    You can assign arrays to hashes in which case the array elements are
    interpreted as key-value pairs. Furthermore the '=>' is just the same as
    a regular comma. So that has assignment can be rewritten as
    %hi = ('hi', (0, 1));
    which is flattened to
    %hi = ('hi', 0, 1);
    which will assign 0 to $hi{hi} and yield a warning about odd number of
    elements because there is no value specified for $hi{1}.

    jue
     
    Jürgen Exner, Aug 24, 2008
    #4
    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. Andrés Giraldo

    IEWebControl TabStrip, scales the Image properties

    Andrés Giraldo, Dec 10, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    135
    Andrés Giraldo
    Dec 10, 2003
  2. rp
    Replies:
    1
    Views:
    582
    red floyd
    Nov 10, 2011
  3. Ruby Maniac

    Ruby Scales just fine !

    Ruby Maniac, Sep 25, 2007, in forum: Ruby
    Replies:
    12
    Views:
    179
    Michael Hollins
    Sep 26, 2007
  4. gavino
    Replies:
    2
    Views:
    113
    Tad McClellan
    Jul 26, 2006
  5. loadspeed

    pop-up that scales to content

    loadspeed, Jun 7, 2005, in forum: Javascript
    Replies:
    3
    Views:
    90
    loadspeed
    Jun 8, 2005
Loading...

Share This Page