File with hash inside it

Discussion in 'Perl Misc' started by phillyfan, Aug 29, 2005.

  1. phillyfan

    phillyfan Guest

    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.
    phillyfan, Aug 29, 2005
    #1
    1. Advertising

  2. phillyfan

    Matija Papec Guest

    X-Ftn-To: phillyfan

    "phillyfan" <> wrote:
    >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};



    --
    Matija
    Matija Papec, Aug 29, 2005
    #2
    1. Advertising

  3. phillyfan

    phillyfan Guest

    How would I show classname, desc, title
    phillyfan, Aug 30, 2005
    #3
  4. phillyfan

    Anno Siegel Guest

    phillyfan <> wrote in comp.lang.perl.misc:
    > How would I show classname, desc, title


    Read Matija's answer again. And provide context and attributions
    with your replies.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Aug 30, 2005
    #4
  5. phillyfan

    Matija Papec Guest

    X-Ftn-To: phillyfan

    "phillyfan" <> wrote:
    >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.


    --
    Matija
    Matija Papec, Aug 30, 2005
    #5
  6. phillyfan

    phillyfan Guest

    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;
    phillyfan, Aug 30, 2005
    #6
  7. "phillyfan" <> wrote in
    news::

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

    > exit 0;


    Unnecessary.

    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, Aug 30, 2005
    #7
  8. phillyfan

    Matija Papec Guest

    On 30 Aug 2005 14:02:14 -0700, "phillyfan" <>
    wrote:

    >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).
    Matija Papec, Sep 1, 2005
    #8
  9. phillyfan

    phillyfan Guest

    Matija Papec wrote:
    > On 30 Aug 2005 14:02:14 -0700, "phillyfan" <>
    > 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.
    phillyfan, Sep 1, 2005
    #9
  10. "phillyfan" <> writes:
    > 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
    Eric Schwartz, Sep 1, 2005
    #10
    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. Red Orchid
    Replies:
    3
    Views:
    1,026
  2. David Dorward
    Replies:
    1
    Views:
    2,108
    Isofarro
    Jun 28, 2003
  3. Leon Mayne
    Replies:
    5
    Views:
    4,739
    Leon Mayne
    May 27, 2008
  4. rp
    Replies:
    1
    Views:
    494
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    596
    David A. Black
    Jul 2, 2008
Loading...

Share This Page