Hash of array access tricks. Sorry, long.

Discussion in 'Perl Misc' started by s. keeling, Oct 14, 2005.

  1. s. keeling

    s. keeling Guest

    I've built a HoA as so:

    my %prompt = (
    FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    qq(You MUST supply a first name.\n) ],
    LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    qq(You MUST supply a last name.\n) ],

    and so on. I'd like to write to and access $prompt->{$key}[2] when
    prompting a user for the name, and I'd like to stuff the reply in
    $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
    I'll try to supply via a call to mysql. :)

    # FIRST_NAME
    #
    my $key = qq(FIRST_NAME);
    print STDOUT qq($prompt->{$key}[2]);
    $prompt->{$key}[1]=<STDIN>;
    chomp($prompt->{$key}[1]);
    if ( ! $prompt->{$key}[1] ) {
    die $prompt->{$key}[3];
    }

    What I get is:

    Use of uninitialized value in string at ./mupdate.pl line 154 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl tells you what operation
    you used the undefined value in. Note, however, that perl optimizes your
    program and the operation displayed in the warning may not necessarily
    appear literally in your program. For example, "that $foo" is
    usually optimized into "that " . $foo, and the warning will refer to
    the concatenation (.) operator, even though there is no . in your
    program.


    Use of uninitialized value in die at ./mupdate.pl line 158, <STDIN> line 1 (#1)

    Died at ./mupdate.pl line 158, <STDIN> line 1 (#2)
    (F) You passed die() an empty string (the equivalent of die "") or
    you called it with no args and both $@ and $_ were empty.

    Uncaught exception from user code:
    Died at ./mupdate.pl line 158, <STDIN> line 1.
    at ./mupdate.pl line 158


    I think I'm using the same sort of thing, using the same syntax as
    Perl Cookbook and Camel say it's to be done. It sort of works when I
    use $prompt{$key}[N] instead. What am I confusing?


    --
    Any technology distinguishable from magic is insufficiently advanced.
    (*) http://www.spots.ab.ca/~keeling Linux Counter #80292
    - - Spammers! http://www.spots.ab.ca/~keeling/autospam.html
    http://www.ietf.org/rfc/rfc1855.txt democracy human rights Taiwan Independence
    s. keeling, Oct 14, 2005
    #1
    1. Advertising

  2. s. keeling

    Guest

    "s. keeling" <> wrote:
    > I've built a HoA as so:
    >
    > my %prompt = (
    > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    > qq(You MUST supply a first name.\n) ],
    > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    > qq(You MUST supply a last name.\n) ],


    %prompt is a lexical hash.

    > $prompt->{$key}[1]=<STDIN>;


    $prompt is an (undeclared?) scalar which is unrelated to %prompt, other
    than that their names look kind of similar. You are trying to derefence
    that scalar as if it held a reference to a hash, which there is no reason
    to believe that it does.

    > What am I confusing?


    The usefulness of "use strict", probably.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Oct 14, 2005
    #2
    1. Advertising

  3. s. keeling

    s. keeling Guest

    s. keeling <>:
    > I've built a HoA as so:
    >
    > my %prompt = (
    > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    > qq(You MUST supply a first name.\n) ],
    > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    > qq(You MUST supply a last name.\n) ],
    >
    > and so on. I'd like to write to and access $prompt->{$key}[2] when
    > prompting a user for the name, and I'd like to stuff the reply in
    > $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
    > I'll try to supply via a call to mysql. :)


    I should also say for some reason it won't work if I don't declare
    both $prompt and %prompt, ... or should I just be "my"ing the former
    and not the latter? I'll try that too.


    --
    Any technology distinguishable from magic is insufficiently advanced.
    (*) http://www.spots.ab.ca/~keeling Linux Counter #80292
    - - Spammers! http://www.spots.ab.ca/~keeling/autospam.html
    http://www.ietf.org/rfc/rfc1855.txt democracy human rights Taiwan Independence
    s. keeling, Oct 14, 2005
    #3
  4. s. keeling

    Paul Lalli Guest

    Re: Hash of array access tricks. Sorry, long.

    s. keeling wrote:
    > s. keeling <>:
    > > I've built a HoA as so:
    > >
    > > my %prompt = (
    > > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    > > qq(You MUST supply a first name.\n) ],
    > > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    > > qq(You MUST supply a last name.\n) ],
    > >
    > > and so on. I'd like to write to and access $prompt->{$key}[2] when
    > > prompting a user for the name, and I'd like to stuff the reply in
    > > $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
    > > I'll try to supply via a call to mysql. :)

    >
    > I should also say for some reason it won't work if I don't declare
    > both $prompt and %prompt, ... or should I just be "my"ing the former
    > and not the latter? I'll try that too.



    Throwing stuff at the wall to see what sticks is a poor method of
    debugging your program. The reason it told you to define $prompt is
    because you were using $prompt intead of the variable you actually
    declared, which was %prompt. $prompt and %prompt are 100% unrelated.

    %prompt ==> hash
    $prompt{$key} ==> value of %hash at $key
    $prompt ==> scalar. Possibly a reference to a hash
    $prompt->{$key} ==> value of the hash referenced by $prompt, at $key

    For more information:
    perldoc perlref

    Paul Lalli
    Paul Lalli, Oct 14, 2005
    #4
  5. s. keeling

    Guest

    Re: Hash of array access tricks. Sorry, long.

    Paul Lalli wrote:
    >
    > Throwing stuff at the wall to see what sticks is a poor method of
    > debugging your program. The reason it told you to define $prompt is
    > because you were using $prompt intead of the variable you actually
    > declared, which was %prompt. $prompt and %prompt are 100% unrelated.



    to be pedantic, aren't $prompt and %prompt related by the fact that
    they are in the same typeglob?

    the typeglob *prompt contains in its symbol table both $prompt and
    %prompt.

    the contents of the variables $prompt and %prompt may be unrelated, but
    the variables themselves are related. i think. i don't know too much
    about typeglobs...
    , Oct 14, 2005
    #5
  6. Re: Hash of array access tricks. Sorry, long.

    wrote in
    news::

    >
    > Paul Lalli wrote:
    >>
    >> because you were using $prompt intead of the variable you actually
    >> declared, which was %prompt. $prompt and %prompt are 100% unrelated.

    >
    > to be pedantic, aren't $prompt and %prompt related by the fact that
    > they are in the same typeglob?
    >
    > the typeglob *prompt contains in its symbol table both $prompt and
    > %prompt.
    >
    > the contents of the variables $prompt and %prompt may be unrelated,
    > but the variables themselves are related. i think. i don't know too
    > much about typeglobs...


    http://www.perl.com/pub/a/2002/05/14/mod_perl.html

    <blockquote>
    When you create a symbol, Perl creates a symbol table entry for
    that symbol in the current package's symbol table (by default main::).
    Each symbol table entry is called a typeglob. Each typeglob can hold
    information on a scalar, an array, a hash, a subroutine (code), a
    filehandle, a directory handle and a format, each of which all have the
    same name. So you see now that there are two indirections for a global
    variable: the symbol (the thing's name) points to its typeglob, and the
    entry in the typeglob for the thing's type (scalar, array, etc.) points
    to the data. If we had a scalar and an array with the same name, then
    their name would point to the same typeglob, but for each type of data
    the typeglob points to somewhere different. Hence, the scalar's data and
    the array's data are completely separate and independent, they just
    happen to have the same name.
    </blockquote>

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 15, 2005
    #6
  7. s. keeling

    Guest

    Re: Hash of array access tricks. Sorry, long.

    wrote:
    > Paul Lalli wrote:
    > >
    > > Throwing stuff at the wall to see what sticks is a poor method of
    > > debugging your program. The reason it told you to define $prompt is
    > > because you were using $prompt intead of the variable you actually
    > > declared, which was %prompt. $prompt and %prompt are 100% unrelated.

    >
    > to be pedantic, aren't $prompt and %prompt related by the fact that
    > they are in the same typeglob?


    If they were package variables I would agree. But because at least one
    of them was lexical, I think not.


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Oct 15, 2005
    #7
  8. s. keeling

    s. keeling Guest

    Re: Hash of array access tricks. Sorry, long.

    Paul Lalli <>:
    > s. keeling wrote:
    > > s. keeling <>:
    > > > I've built a HoA as so:
    > > >
    > > > my %prompt = (
    > > > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    > > > qq(You MUST supply a first name.\n) ],
    > > > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    > > > qq(You MUST supply a last name.\n) ],
    > > >
    > > > and so on. I'd like to write to and access $prompt->{$key}[2] when
    > > > prompting a user for the name, and I'd like to stuff the reply in
    > > > $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
    > > > I'll try to supply via a call to mysql. :)

    > >
    > > I should also say for some reason it won't work if I don't declare
    > > both $prompt and %prompt, ... or should I just be "my"ing the former

    >
    > Throwing stuff at the wall to see what sticks is a poor method of


    Granted. It was just reflex.

    > debugging your program. The reason it told you to define $prompt is
    > because you were using $prompt intead of the variable you actually
    > declared, which was %prompt. $prompt and %prompt are 100% unrelated.

    ..................................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    > %prompt ==> hash
    > $prompt{$key} ==> value of %hash at $key


    Do you not see the relationship?!?

    > $prompt ==> scalar. Possibly a reference to a hash
    > $prompt->{$key} ==> value of the hash referenced by $prompt, at $key


    Yes, and I'm trying to get to an element of array $key, $prompt->{$key}[2]

    > For more information:
    > perldoc perlref


    Will do, have done, will do again. Thanks.


    --
    Any technology distinguishable from magic is insufficiently advanced.
    (*) http://www.spots.ab.ca/~keeling Linux Counter #80292
    - - Spammers! http://www.spots.ab.ca/~keeling/autospam.html
    http://www.ietf.org/rfc/rfc1855.txt democracy human rights Taiwan Independence
    s. keeling, Oct 16, 2005
    #8
  9. s. keeling

    s. keeling Guest

    Lawrence Statton N1GAK/XE2 <-here.net>:
    > "s. keeling" <> writes:
    >
    > > s. keeling <>:
    > > > I've built a HoA as so:
    > > >
    > > > my %prompt = (
    > > > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    > > > qq(You MUST supply a first name.\n) ],
    > > > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    > > > qq(You MUST supply a last name.\n) ],
    > > >
    > > > and so on. I'd like to write to and access $prompt->{$key}[2] when
    > > > prompting a user for the name, and I'd like to stuff the reply in
    > > > $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
    > > > I'll try to supply via a call to mysql. :)

    > >
    > > I should also say for some reason it won't work if I don't declare
    > > both $prompt and %prompt, ... or should I just be "my"ing the former

    >
    > perhaps this will help you understand...
    >
    > #!/usr/bin/perl


    Well,it adds to the mystery.

    > %prompt at $prompt are different variables.... Just WISHING $prompt
    > to become a refrence to %prompt will not make it so.


    It seems my books disagreed, as I understood them. Now I see a
    construct I hadn't seen necessary 'til now: @{$prompt->{$key}[2]}. Thx.



    --
    Any technology distinguishable from magic is insufficiently advanced.
    (*) http://www.spots.ab.ca/~keeling Linux Counter #80292
    - - Spammers! http://www.spots.ab.ca/~keeling/autospam.html
    http://www.ietf.org/rfc/rfc1855.txt democracy human rights Taiwan Independence
    s. keeling, Oct 16, 2005
    #9
  10. Re: Hash of array access tricks. Sorry, long.

    "s. keeling" <> wrote in
    news::

    > Paul Lalli <>:
    >> s. keeling wrote:
    >> > s. keeling <>:
    >> > > I've built a HoA as so:
    >> > >
    >> > > my %prompt = (
    >> > > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name ->
    >> > > ),
    >> > > qq(You MUST supply a first name.\n) ],
    >> > > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name ->
    >> > > ),
    >> > > qq(You MUST supply a last name.\n) ],
    >> > >
    >> > > and so on. I'd like to write to and access $prompt->{$key}[2]
    >> > > when prompting a user for the name, and I'd like to stuff the
    >> > > reply in $prompt{$key}[1], then compare that with
    >> > > $prompt->{$key}[0], which I'll try to supply via a call to
    >> > > mysql. :)
    >> >
    >> > I should also say for some reason it won't work if I don't declare
    >> > both $prompt and %prompt, ... or should I just be "my"ing the
    >> > former

    >>
    >> Throwing stuff at the wall to see what sticks is a poor method of

    >
    > Granted. It was just reflex.
    >
    >> debugging your program. The reason it told you to define $prompt is
    >> because you were using $prompt intead of the variable you actually
    >> declared, which was %prompt. $prompt and %prompt are 100%
    >> unrelated.

    > .................................

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > ^
    >>
    >> %prompt ==> hash
    >> $prompt{$key} ==> value of %hash at $key

    >
    > Do you not see the relationship?!?


    There is no relationship between the scalar variable $prompt and $prompt
    {$key}.

    >> $prompt ==> scalar. Possibly a reference to a hash
    >> $prompt->{$key} ==> value of the hash referenced by $prompt, at $key

    >
    > Yes, and I'm trying to get to an element of array $key,
    > $prompt->{$key}[2]


    Here is the problem you are having: You have a hash, %prompt, and you
    are expecting that $prompt will be a reference to that hash. That is not
    so, for all the reasons explained in this thread. You have two options:
    Ditch %prompt, and declare $prompt to be a reference to an anonymous
    hash containing the data you want:

    my $prompt = {
    FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    qq(You MUST supply a first name.\n) ],
    LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    qq(You MUST supply a last name.\n) ],
    };

    Then, $prompt->{FIRST_NAME}->[2] will be "\tLast name ->".

    Alternatively, if you want to keep %prompt, but want to access it using
    a reference to it in $prompt, you'll need to explicitly say:

    my $prompt = \%prompt;

    Otherwise, there is no relationship between $prompt and %prompt.

    Creating a hash called %prompt does not automatically create a reference
    to it by the name $prompt.

    Sinan

    PS: Setting X-No-Archive: yes is a good way of making your way into many
    killfiles.

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 16, 2005
    #10
  11. Re: Hash of array access tricks. Sorry, long.

    s. keeling wrote:
    > Paul Lalli <>:
    > > s. keeling wrote:
    > > > s. keeling <>:
    > > > > I've built a HoA as so:
    > > > >
    > > > > my %prompt = (
    > > > > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    > > > > qq(You MUST supply a first name.\n) ],
    > > > > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    > > > > qq(You MUST supply a last name.\n) ],
    > > > >
    > > > > and so on. I'd like to write to and access $prompt->{$key}[2] when
    > > > > prompting a user for the name, and I'd like to stuff the reply in
    > > > > $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
    > > > > I'll try to supply via a call to mysql. :)
    > > >
    > > > I should also say for some reason it won't work if I don't declare
    > > > both $prompt and %prompt, ... or should I just be "my"ing the former

    > >
    > > Throwing stuff at the wall to see what sticks is a poor method of

    >
    > Granted. It was just reflex.
    >
    > > debugging your program. The reason it told you to define $prompt is
    > > because you were using $prompt intead of the variable you actually
    > > declared, which was %prompt. $prompt and %prompt are 100% unrelated.

    > .................................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > >
    > > %prompt ==> hash
    > > $prompt{$key} ==> value of %hash at $key

    >
    > Do you not see the relationship?!?
    >
    > > $prompt ==> scalar. Possibly a reference to a hash
    > > $prompt->{$key} ==> value of the hash referenced by $prompt, at $key

    >
    > Yes, and I'm trying to get to an element of array $key, $prompt->{$key}[2]
    >
    > > For more information:
    > > perldoc perlref

    >
    > Will do, have done, will do again. Thanks.


    I'm fairly certain Paul sees the relation ship between:
    %prompt and $prompt{$key}. the former is a hash, and the latter
    represents the scalar ($) value associated with the key ($key) of the
    hash (%prompt).

    but re: the scenario you presented, you were attempting to relate:
    %prompt (hash)
    and
    $prompt (from the context, you expected $prompt to be a hash reference)

    ....i believe you are confused because of the name. to access a value in
    $prompt (assuming it was a valid hash reference), you would need to
    write:
    $prompt->{$key}

    an analogy that i can think of which may help you out is this: i have a
    token which can get me on the subway. i have named this token 'Max'. i
    also have a voucher for a token that can get me on the subway. i call
    this voucher 'Max' as well. these two items can get me to my goal
    (getting on the subway), and they have the same name, but they are 2
    different types of things. one can grant me direct access to the
    subway, and the other can get me the thing that can get me on the
    subway.

    please correct me if i'm mangling this analogy...
    it_says_BALLS_on_your forehead, Oct 16, 2005
    #11
  12. s. keeling

    s. keeling Guest

    Re: Hash of array access tricks. Sorry, long. Thanks!

    it_says_BALLS_on_your forehead <>:
    >
    > s. keeling wrote:
    > > Paul Lalli <>:
    > > > s. keeling wrote:
    > > > > s. keeling <>:
    > > > > > I've built a HoA as so:
    > > > > >
    > > > > > my %prompt = (
    > > > > > FIRST_NAME => [ qq(NULL), qq(NULL), qq(\tFirst name -> ),
    > > > > > qq(You MUST supply a first name.\n) ],
    > > > > > LAST_NAME => [ qq(NULL), qq(NULL), qq(\tLast name -> ),
    > > > > > qq(You MUST supply a last name.\n) ],
    > > > > >
    > > > > > and so on. I'd like to write to and access $prompt->{$key}[2] when
    > > > > > prompting a user for the name, and I'd like to stuff the reply in
    > > > > > $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
    > > > > > I'll try to supply via a call to mysql. :)
    > > > >
    > > > > I should also say for some reason it won't work if I don't declare
    > > > > both $prompt and %prompt, ... or should I just be "my"ing the former
    > > >
    > > > debugging your program. The reason it told you to define $prompt is
    > > > because you were using $prompt intead of the variable you actually
    > > > declared, which was %prompt. $prompt and %prompt are 100% unrelated.

    > > .................................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > > >
    > > > %prompt ==> hash
    > > > $prompt{$key} ==> value of %hash at $key

    > >
    > > Do you not see the relationship?!?
    > >
    > > > $prompt ==> scalar. Possibly a reference to a hash
    > > > $prompt->{$key} ==> value of the hash referenced by $prompt, at $key

    > >
    > > Yes, and I'm trying to get to an element of array $key, $prompt->{$key}[2]
    > >
    > > > For more information:
    > > > perldoc perlref

    > >
    > > Will do, have done, will do again. Thanks.

    >
    > I'm fairly certain Paul sees the relation ship between:
    > %prompt and $prompt{$key}. the former is a hash, and the latter
    > represents the scalar ($) value associated with the key ($key) of the
    > hash (%prompt).
    >
    > but re: the scenario you presented, you were attempting to relate:
    > %prompt (hash)
    > and
    > $prompt (from the context, you expected $prompt to be a hash reference)


    I was going from things I was looking at in books thinking:

    my %hash;
    $hash{$key}[$subs] = qq(blah!);

    Which works.

    I was unaware of the other side of this; a reference to a hash?

    my %hash;
    $hash = \%hash;
    @{$hash->{$key}[$subs]} = qq(blah!);

    I think. I was trying to concentrate on the former method.

    > ...i believe you are confused because of the name. to access a value in
    > $prompt (assuming it was a valid hash reference), you would need to
    > write:
    > $prompt->{$key}


    Many thanks for everybody's input on this. I've got it working
    splendidly now.


    --
    Any technology distinguishable from magic is insufficiently advanced.
    (*) http://www.spots.ab.ca/~keeling Linux Counter #80292
    - - Spammers! http://www.spots.ab.ca/~keeling/autospam.html
    http://www.ietf.org/rfc/rfc1855.txt democracy human rights Taiwan Independence
    s. keeling, Oct 20, 2005
    #12
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    674
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,186
    Peter Shaggy Haywood
    Sep 20, 2005
  3. veryhotsausage
    Replies:
    1
    Views:
    1,800
    veryhotsausage
    Jul 4, 2008
  4. rp
    Replies:
    1
    Views:
    520
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    617
    David A. Black
    Jul 2, 2008
Loading...

Share This Page