File with hash inside it

P

phillyfan

I have a file containing the following info:

{
'MGT6101' => {
'desc' => 'An examination of the tools and procedures used by
organizations to attract, select, and retain employees within the
context of the legal and regulatory environment.',
'title' => 'Managing Human Resources'
},
'MGT6015' => {
'desc' => 'The course covers cost estimation, standard costs,
variable costing, relevant costs, transfer pricing, performance
evaluation, cost of quality, and activity-based costing for service.',
'title' => 'Managerial Accounting II'
}

}

There are more lines in the file like these but you get the point. I
would like to be able to go thru the file and find the classname I want
(MGT6101) match it with another table's field and then do what I want
with the dec and title from MGT6101. Any ideas.
 
M

Matija Papec

X-Ftn-To: phillyfan

phillyfan said:
There are more lines in the file like these but you get the point. I
would like to be able to go thru the file and find the classname I want
(MGT6101) match it with another table's field and then do what I want
with the dec and title from MGT6101. Any ideas.

Looks like your file has valid perl hashref. If that is the case you can,

my $href = do "yourfile.ext";
print $href->{MGT6101}{title};
 
M

Matija Papec

X-Ftn-To: phillyfan

phillyfan said:
How would I show classname, desc, title

if you want keys from your hashref then it's pretty straightforward
print join ",", keys %$href;

you might want to check "HASHES OF HASHES" in perldoc perldsc.

ps. quoting some part of previous post isn't only considered polite, but it
also helps others to know what are you referring to.
 
P

phillyfan

Thanks Matija for your help. I should have quoted as you say. I
figured out how to display the key, classname, and the values desc, and
title. Thanks your ps made a lot more sense then the previous posters.
answer. I ended up with:

my $href = do "data-courses.do";

open(OUT,">hash-table.txt");
while( ( $key, $value) = each(%$href) ) {
while( ($value1, $value2) = each %$value ){
if($value1 eq 'title'){next}
print OUT "$key \t $value1 \t $value2 \n";

}

}close(OUT);
exit 0;
 
A

A. Sinan Unur

I should have quoted as you say.

And yet you still do not quote. Hmmm ...
open(OUT,">hash-table.txt");

Always, yes *always*, check if open succeeded:

open my $out, '>', 'hash-table.txt'
or die "Cannot open 'hash-table.txt for writing: $!";
while( ( $key, $value) = each(%$href) ) {
while( ($value1, $value2) = each %$value ){

I am not even going to presume to understand what you are doing here.
if($value1 eq 'title'){next}

More Perl-like:

next if $value1 eq 'title';
}close(OUT);

Do check if close succeded as well.

Unnecessary.

Sinan
 
M

Matija Papec

Thanks Matija for your help. I should have quoted as you say. I
figured out how to display the key, classname, and the values desc, and
title. Thanks your ps made a lot more sense then the previous posters.

That is because some people are more regular at clpm, so they got
tired of repeating themselves, same things day after day. ;)
If you want this group to stay informative and helpful, read some
older topics via google, read posting guidelines and FAQ.
answer. I ended up with:

my $href = do "data-courses.do";

open(OUT,">hash-table.txt");
while( ( $key, $value) = each(%$href) ) {
while( ($value1, $value2) = each %$value ){

($key1, $value1) would be more consistent (consider that someone could
maintain this code after you leave your current position).
 
P

phillyfan

Matija said:
On 30 Aug 2005 14:02:14 -0700, "phillyfan" <[email protected]>
wrote:


That is because some people are more regular at clpm, so they got
tired of repeating themselves, same things day after day. ;)
If you want this group to stay informative and helpful, read some
older topics via google, read posting guidelines and FAQ.


($key1, $value1) would be more consistent (consider that someone could
maintain this code after you leave your current position).

Ok I have this quote thing figured out. Thanks for the tip. As you can
tell I haven't messed with hash tables before. Just getting back into
coding again, as you can tell. It's starting to come back. I
understand the frustration of repeating the same humdrum and will try
not to be a burden on the mongers. I know efficient code is the key
and will get it down. This is just a test scrip and not the finished
product which is why it is sans comments and the sort. thanks once
again. A little kick in the intellectual pants has done me justice.
 
E

Eric Schwartz

phillyfan said:
I know efficient code is the key and will get it down.

Oh goodness no, please! Get it right first, and *then* worry about
getting it efficient. "Premature optimiztion is the root of all
evil", after all.

-=Eric
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top