Really basic newbie question about hashes

Discussion in 'Perl Misc' started by Leslie Houk, Oct 14, 2005.

  1. Leslie Houk

    Leslie Houk Guest

    This is simplified, but shows what I want to do. I have the following
    hash:

    %TAGS = (
    org_code => {
    'A',
    'B',
    'C',
    'D',
    },
    priority => {
    '1',
    '2',
    '3',
    '4',
    };
    budgeted => {
    'yes',
    'no',
    'TBD',
    };
    );

    I want to print out either "A,B,C,D", "1,2,3,4", or "yes,no,TBD" based
    on whether $foo is "org_code", "priority", or "budgeted". I tried

    foreach $value ( @TAGS{$foo} ) {
    print( "$value\n" );
    }

    but it didn't work. What should I have put in my foreach? (I'm
    running perl v5.8.3, if it matters.) Thank you in advance to all who
    respond.
     
    Leslie Houk, Oct 14, 2005
    #1
    1. Advertising

  2. Leslie Houk

    Scott Bryce Guest

    Leslie Houk wrote:

    > This is simplified, but shows what I want to do. I have the following
    > hash:
    >
    > %TAGS = (
    > org_code => {
    > 'A',
    > 'B',
    > 'C',
    > 'D',
    > },
    > priority => {
    > '1',
    > '2',
    > '3',
    > '4',
    > };
    > budgeted => {
    > 'yes',
    > 'no',
    > 'TBD',
    > };
    > );


    This is not the correct syntax to create a hash of arrays. In fact, it
    contains syntax errors. Please post real code.


    > I want to print out either "A,B,C,D", "1,2,3,4", or "yes,no,TBD" based
    > on whether $foo is "org_code", "priority", or "budgeted". I tried
    >
    > foreach $value ( @TAGS{$foo} ) {
    > print( "$value\n" );
    > }


    The hash contains references to anonymous arrays. These references need
    to be dereferenced.


    > but it didn't work.


    It doesn't even compile.

    use strict;
    use warnings;

    my %TAGS = (
    org_code => [
    'A',
    'B',
    'C',
    'D'
    ],
    priority => [
    '1',
    '2',
    '3',
    '4'
    ],
    budgeted => [
    'yes',
    'no',
    'TBD'
    ]
    );

    my $foo = 'priority';

    foreach my $value (@{$TAGS{$foo}}) {
    print( "$value\n" );
    }


    http://perldoc.perl.org/perldsc.html#HASHES-OF-ARRAYS
    http://perldoc.perl.org/perldsc.html#Access-and-Printing-of-a-HASH-OF-ARRAYS
     
    Scott Bryce, Oct 14, 2005
    #2
    1. Advertising

  3. Leslie Houk <> wrote:

    > I have the following
    > hash:
    >
    > %TAGS = (



    You should always enable strictures in Perl programs:

    use strict;


    Then that should be:

    my %TAGS = (


    > org_code => {
    > 'A',
    > 'B',
    > 'C',
    > 'D',
    > },



    Most people would write that like this:

    org_code => {
    A => 'B',
    C => 'D'
    },

    It that what you wanted the inner hash to look like?


    > priority => {
    > '1',
    > '2',
    > '3',
    > '4',
    > };

    ^
    ^ syntax error

    Please post *real* Perl code!

    Have you seen the Posting Guidelines that are posted here frequently?


    > budgeted => {
    > 'yes',
    > 'no',
    > 'TBD',



    You should always enable warnings when developing Perl code:

    use warnings;

    Did you really want to have an undef value in your hash?


    > };
    > );
    >
    > I want to print out either "A,B,C,D", "1,2,3,4", or "yes,no,TBD" based
    > on whether $foo is "org_code", "priority", or "budgeted".



    foreach my $key ( keys %TAGS ) {
    print "key=$key: ", join(',', %{ $TAGS{$key} }), "\n";
    }


    See also:

    perlreftut
    perlref
    perllol
    perldsc


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 14, 2005
    #3
  4. Leslie Houk wrote:
    > This is simplified, but shows what I want to do. I have the following
    > hash:
    >
    > %TAGS = (
    > org_code => {
    > 'A',
    > 'B',
    > 'C',
    > 'D',
    > },
    > priority => {
    > '1',
    > '2',
    > '3',
    > '4',
    > };
    > budgeted => {
    > 'yes',
    > 'no',
    > 'TBD',
    > };
    > );


    Then your program does not compile.

    Please copy and paste code that you post here, don't retype it. Have you
    seen the posting guidelines for this group?
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    > I want to print out either "A,B,C,D", "1,2,3,4", or "yes,no,TBD" based
    > on whether $foo is "org_code", "priority", or "budgeted". I tried
    >
    > foreach $value ( @TAGS{$foo} ) {
    > print( "$value\n" );
    > }


    What do you think the hash contains? Do you realize that, leaving your
    typos aside, it contains hash references?

    > but it didn't work. What should I have put in my foreach?


    Before going into that, please explain more carefully what it is you are
    trying to do. Also, it would be a good idea to read up on references and
    data structures in Perl.

    perldoc perlreftut
    perldoc perlref
    perldoc perldsc

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 14, 2005
    #4
  5. Leslie Houk

    Guest

    Return of troll (was Re: Really basic newbie question about hashes)

    Kiralynne Schilitubi (AKA Purl Gurl AKA Kira AKA Godzilla!) wrote:

    <tendentious and poor Perl advice deleted>

    I think it may be eleven months since Kira last posted in CLPM. Though
    she's been sporadically active since at least 1999.

    Sample:
    "Bottom line, there are lots and lots of mentally ill
    criminals in the Perl Community who have stalking and
    harassing our family, for years. A majority of posters
    here, are those mentally ill criminals. "
    - Purl Gurl, comp.lang.perl.misc, Jun 28 2004, 10:10 pm

    I'd review Kira's previous postings before acting on her Perl advice.
    She's picked ludicrous fights with most people who know Perl well and
    consequently is in numerous killfiles.
     
    , Oct 15, 2005
    #5
  6. Leslie Houk

    Leslie Houk Guest

    Scott Bryce wrote:
    > Leslie Houk wrote:
    >
    > > This is simplified, but shows what I want to do. I have the following
    > > hash:
    > >
    > > %TAGS = (
    > > org_code => {
    > > 'A',
    > > 'B',
    > > 'C',
    > > 'D',
    > > },
    > > priority => {
    > > '1',
    > > '2',
    > > '3',
    > > '4',
    > > };
    > > budgeted => {
    > > 'yes',
    > > 'no',
    > > 'TBD',
    > > };
    > > );

    >
    > This is not the correct syntax to create a hash of arrays. In fact, it
    > contains syntax errors. Please post real code.
    >
    > > I want to print out either "A,B,C,D", "1,2,3,4", or "yes,no,TBD" based
    > > on whether $foo is "org_code", "priority", or "budgeted". I tried
    > >
    > > foreach $value ( @TAGS{$foo} ) {
    > > print( "$value\n" );
    > > }

    >
    > The hash contains references to anonymous arrays. These references need
    > to be dereferenced.
    >
    > > but it didn't work.

    >
    > It doesn't even compile.
    >
    > use strict;
    > use warnings;
    >
    > my %TAGS = (
    > org_code => [
    > 'A',
    > 'B',
    > 'C',
    > 'D'
    > ],
    > priority => [
    > '1',
    > '2',
    > '3',
    > '4'
    > ],
    > budgeted => [
    > 'yes',
    > 'no',
    > 'TBD'
    > ]
    > );
    >
    > my $foo = 'priority';
    >
    > foreach my $value (@{$TAGS{$foo}}) {
    > print( "$value\n" );
    > }
    >
    >
    > http://perldoc.perl.org/perldsc.html#HASHES-OF-ARRAYS
    > http://perldoc.perl.org/perldsc.html#Access-and-Printing-of-a-HASH-OF-ARRAYS


    Thank you! I had never heard of hashes until a couple of days ago, and
    was trying to figure out how to use them by looking at examples in this
    newsgroup. Obviously, I did not do a very good job, as shown by my
    above code -- yes, that was the real code I was trying to run. I
    appreciate your taking the time to show me how to do it correctly.
     
    Leslie Houk, Oct 15, 2005
    #6
  7. Purl Gurl <> wrote:
    > Tad McClellan wrote:
    >> Leslie Houk <> wrote:

    >
    > (snipped)
    >
    >>> %TAGS = (

    >
    >> use strict;

    >
    >> Then that should be:

    >
    >> my %TAGS = (

    >
    > %TAGS is global.



    No it isn't.

    It will work just fine with a lexical %TAGS.


    > our %TAGS ...



    You should always prefer lexical (my) variables over package (our)
    variables, except when you can't.

    A file-scoped %TAGS is all that is needed, so my code is more
    robust than yours which introduces package variables when they
    are not necessary.


    > Use of "our" and "my" for globals serves no purpose.



    Just because you do not understand the difference does not mean
    that there is not a difference.

    my() does not make global variables, it makes lexical variables.

    Saying "our and my for globals" indicates that you have some misunderstanding
    with regards to Perl's two different systems of variables.



    For those interested in actually understanding the difference, see:

    "Coping with Scoping":

    http://perl.plover.com/FAQs/Namespaces.html


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 15, 2005
    #7
  8. Leslie Houk

    Kegs Guest

    Purl Gurl <> writes:

    > I am curious how you read my mind, how you know my extent of knowledge.


    Because your posting history in this group has the extent of your
    knowledge overwhelmingly clear to all but newbies who don't know the
    language well enough yet.

    --
    James jamesk[at]homeric[dot]co[dot]uk

    I bought a house on a one-way dead-end road. I don't know how I got there.
    (Steven Wright)
     
    Kegs, Oct 15, 2005
    #8
  9. Leslie Houk

    Matt Garrish Guest

    "Kegs" <> wrote in message
    news:...
    > Purl Gurl <> writes:
    >
    >> I am curious how you read my mind, how you know my extent of knowledge.

    >
    > Because your posting history in this group has the extent of your
    > knowledge overwhelmingly clear to all but newbies who don't know the
    > language well enough yet.
    >


    She's just trolling to cover yet another kira mistake. Don't waste your time
    following up, because all she's trying to do is distract people away from
    the fact that she was wrong by going off on tangents unrelated to the
    original issue. It's an old ploy that's far less effectual than she seems to
    believe.

    Matt
     
    Matt Garrish, Oct 15, 2005
    #9
  10. Purl Gurl <> wrote:
    > Tad McClellan wrote:
    >> Purl Gurl wrote:
    >>>Tad McClellan wrote:
    >>>>Leslie Houk <> wrote:

    >
    > (snipped)
    >
    >
    >>>>>%TAGS = (

    >
    >>>>use strict;

    >
    >>>>Then that should be:

    >
    >>>>my %TAGS = (

    >
    >>>%TAGS is global.

    >
    >> No it isn't.

    >
    > %TAGS is global. This is most obvious, even to newbies.



    No it isn't. Despite your claims otherwise.


    >>>our %TAGS ...

    >
    >
    >> You should always prefer lexical (my) variables over package (our)
    >> variables, except when you can't.

    >
    > Readers would benefit by your discussing why you, personally, prefer
    > certain syntax over others.



    It isn't the syntax, it is the semantics.


    > Readers will also benefit by your discussing
    > when you "can't" per your statement.



    When the variable is a built-in variable.

    When you want a global variable. (ie. one that is visible in another
    source code file)

    When you are doing guru-level mucking about with the symbol table.


    >You have a habit of stating what
    > readers can and cannot do, but rarely offer reason why.



    If they read the article I referenced, they would get all that.

    Not much point in restating it when it has already been stated elsewhere.


    > Is it your
    > intent to dictate or to teach?



    To teach, obviously, that is why I included a link that teaches
    the difference.


    >> A file-scoped %TAGS is all that is needed, so my code is more
    >> robust than yours which introduces package variables when they
    >> are not necessary.

    >
    > Your "file-scoped" expression, is not that a semantic lie for "global" variable?



    It depends on if the program is in in single file or not.

    We did not see the OP's complete program so we cannot know which it is.


    >>>Use of "our" and "my" for globals serves no purpose.

    >
    >> Just because you do not understand the difference does not mean
    >> that there is not a difference.

    >
    > What factual basis do you have for your statement?



    Because you said that my() is a global variable and it is not.

    A global variable can be accessed from *anywhere*.

    A lexical variable can NOT be accessed from a different file,
    so it is not global.


    > Are you a mind reader?



    Don't need to be, you clearly think that file-scoped is the same
    as "global" when it isn't.


    > Your falling back upon personal insult is a rather lame tactic employed



    I did not insult you.

    I said that you did not understand the difference between lexical
    variables and package variables. And you don't!


    > by those seeking to cover for a lack of knowledge, a tactic used by trolls.



    You espoused your lack of knowledge authoritatively and I didn't want
    any other readers to be taken in by your lack of understanding.


    >> Saying "our and my for globals" indicates that you have some misunderstanding
    >> with regards to Perl's two different systems of variables.



    No insult there, just an obvious fact.


    > End result is you are displaying a lack of knowledge and experience,



    No, it is you displaying the lack. (and me offering you a chance
    to un-lack :)


    > with your classic display of a lack of self-confidence.



    Because I know what I am talking about and you don't.


    > This is typical of
    > many long time posters here; an effort to conceal problems with Perl rather
    > than share knowledge about problems and how to deal with problems.



    Why don't you just learn the difference so that you *will* be
    qualified to instruct others in the difference?


    > If a purpose is served by lexical declaration of global variables, it is to
    > compensate for broken code within the strict module,



    Even a freshman CS student knows that global variables are "bad".
    They lead to "action at a distance", a very hard-to-troubleshoot
    class of errors.

    Restricting the scope of a variable has been a common practice by
    real programmers for decades, please catch up.


    > I am curious how you read my mind, how you know my extent of knowledge.


    I didn't read your mind, I read your post.

    Your post displayed that the extent of your knowledge was limited,
    I provided a means for you to overcome that limitation.

    Whether you choose to do that, or to remain in ignorance is up to you,
    but I'm not going to let you lead others off into the woods...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 15, 2005
    #10
  11. Purl Gurl wrote:
    > falling back upon personal insult is a rather lame tactic employed
    > by those seeking to cover for a lack of knowledge, a tactic used by trolls.


    Purl Gurl wrote:
    > Periodically, that mule's ass would talk back to me, producing
    > the same nature of words as you. An ass is an ass, yes?



    This particular Troll knows little about Perl.
     
    foo bar baz qux, Oct 16, 2005
    #11
  12. Leslie Houk

    Joe Smith Guest

    Purl Gurl wrote:
    > that mule stood eighty hands high


    Really? 80 hands = 80 x 4 inches = 320 inches = 26 feet 8 inches.
    You cannot expect us to believe your mule was nearly 27 feet tall.
     
    Joe Smith, Oct 16, 2005
    #12
  13. Joe Smith <> wrote:
    > Purl Gurl wrote:
    >> that mule stood eighty hands high

    >
    > Really? 80 hands = 80 x 4 inches = 320 inches = 26 feet 8 inches.
    > You cannot expect us to believe your mule was nearly 27 feet tall.



    That ass is bigger than even J-Lo's !!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 16, 2005
    #13
    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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    590
    Ben Holness
    Oct 8, 2003
  2. =?Utf-8?B?TFc=?=
    Replies:
    8
    Views:
    445
    =?Utf-8?B?TFc=?=
    Apr 25, 2007
  3. LW
    Replies:
    1
    Views:
    192
    Alexey Smirnov
    May 2, 2007
  4. djacober

    Question about hashes of hashes

    djacober, Jul 24, 2005, in forum: Perl Misc
    Replies:
    10
    Views:
    202
    Ian Wilson
    Jul 25, 2005
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    235
Loading...

Share This Page