Really basic newbie question about hashes

L

Leslie Houk

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

Scott Bryce

Leslie said:
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
 
T

Tad McClellan

Leslie Houk said:
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
 
G

Gunnar Hjalmarsson

Leslie said:
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
 
F

foobarbazqux

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

Leslie Houk

Scott said:
Leslie said:
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.
 
T

Tad McClellan

Purl Gurl said:
%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
 
K

Kegs

Purl Gurl said:
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.
 
M

Matt Garrish

Kegs said:
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
 
T

Tad McClellan

Purl Gurl said:
%TAGS is global. This is most obvious, even to newbies.


No it isn't. Despite your claims otherwise.

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.

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.

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.



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

foo bar baz qux

Purl said:
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 said:
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.
 
J

Joe Smith

Purl said:
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.
 
T

Tad McClellan

Joe Smith said:
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 !!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top