in reply to Re: Putting Hash values into an array
in thread Putting Hash values into an array

Thank you for your response! Yes, I have all those typical things at the top, so no problem there. The only data that is coming in that isn't listed, is from the 2009.txt file. The information is two columns: Names of people which becomes the key of the %Diag hash called "$RID" and diagnosis codes that are the values labelled "$DiagCode". I fixed the typo with the file name. It should be "Output by RID".

Now for the technical stuff. So, I understant what you are saying, the $RID should not have a "my" in front of it even though I'm using strict, because that makes it a new variable. Check, removed "my"

I re-opened the file because I was thinking that the script wrote the hash to "Output by RID" then I closed it, then I would reopen it and essentially rename the $DiagCode that were in that file with their technical name using the @Search %Frequency lines. So, I guess that is the wrong way of doing it. Can you please give me a suggestion on how I can import that 2009.txt file into the hash and then rename just the $DiagCode to their technical names using the @Search %frequency?

Finally how would I print everything? Do you just keep appending to the file? As noted in my original question, I need the format to look like the above, but I'm not sure how to merge things. I tried printing a block with EOF, but it didn't work. I tried printing them to different files and merging them, I tried appending the file a bunch of times, but surely there is an easier way! Thanks so much for your help.

  • Comment on Re^2: Putting Hash values into an array

Replies are listed 'Best First'.
Re^3: Putting Hash values into an array
by Athanasius (Archbishop) on Mar 01, 2014 at 13:53 UTC

    My advice is to set up your data structures so as to make data access as simple and straightforward as possible. One of Perl’s greatest strengths is its highly-efficient built-in hashes, so use them to construct lookup tables for the required data. Here is how I would go about this:

    P.S. In future, please supply actual sample data (not a description of what the data looks like — an actual sample of the data) together with the actual output which should result from the given input data. This will make it much easier for the Monks to help you. See How do I post a question effectively?

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Yes that helps so much! Thank you. Sorry for the confusion with the data, but due to the sensitivity of my data (HIPPA laws), I can't display any of it so, that why I just have to give examples, but I will try to give better ones in the future. I will try that code out thank you so much again.

      Thank you so much for your response. I spent all day yesterday reformatting my code to you script. When I ran it thought, it gave me an error. I searched the error and tried some of their fixes, but with no luck. The error is:

      "exists argument is not a HASH or ARRAY element or a subroutine" and it referring to your line "if (exists my $DiagCodes{$_})".

      Do you know how to fix this? Since your script ran so well, I am hoping its something simple, but so far I have had no luck in figuring it out. Thank you so much for your help.

        Hello Raya4505, you wrote:

        your line "if (exists my $DiagCodes{$_})"

        But my line is:

        if (exists $DiagCodes{$_})

        — without the my! When you use my, you declare a new lexical variable. (And if the variable is not explicitly initialised in the declaration, it receives Perl’s default initialisation: undef if it’s a scalar, the empty list if it’s an array or hash.) So, remove the my and it should work correctly.

        Here’s a useful reference: Coping with Scoping by Dominus.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,