Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

A moment with Perl

by Lady_Aleena (Curate)
on Oct 06, 2007 at 22:36 UTC ( #643179=perlmeditation: print w/replies, xml ) Need Help??

This morning I was working on building a table from a hash. I tried doing it on my own, but I just got confused so asked for help. Several monks swung into action to assist, bless them. About that time my beloved wanted to use the computer, so I had to move. Being a night person, I was also a bit sleepy; so when he got on the computer, I went to bed. After I woke up and logged onto Perl Monks, I saw I got a solution. I put it into action after having to do a little fiddling of my own. The results were not as I expected.

I was looking at the results and wondering why they were the way they are. I thought I had a hash of arrays, so that is what I called it when getting the above help. The words "array" and "sort" were floating around my mind. That is when I remembered reading that arrays are sorted.

Okay, so not only did my hash keys get sorted so did my array values, but then another thing hit me, they are not arrays at all. They are hashes just without the value names and fat commas.

That made me think about what I really wanted out of the script I am attempting to write. I realized that this was not just a simple hash of hashes. It needs to be a hash of hashes of arrays.

All of the above thinking took place in less than a minute after I got the results. I was a little tickled with myself for figuring this out on my own. I usually run to the chatterbox as if the sky were falling.

So, here I sit; knowing what I wanted this to be; and not knowing how to make it work as is, if that is even possible. I have already begun researching it, but any pointers of where to look or what search terms to use would be appreciated.

Lady Aleena

"An it harm none, do as ye will."

Replies are listed 'Best First'.
Re: A moment with Perl
by liverpole (Monsignor) on Oct 07, 2007 at 00:08 UTC
    Hi Lady_Aleena,

    If you haven't done so already, take a look at perldata.

    Also, (again, if this is new to you), look into using Data::Dumper.  It's a fantastic, simple way of verifying exactly what a given data structure looks like.

    For example:

    use strict; use warnings; use Data::Dumper; my $pdata = { 'red' => [ 'abc', 'def', 'ghi' ], 'blue' => { '123' => '456', 'xxx' => 'yyy' }, }; print "Dump of pdata = ", Dumper($pdata), "\n";

    which prints:

    Dump of pdata = $VAR1 = { 'blue' => { '123' => '456', 'xxx' => 'yyy' }, 'red' => [ 'abc', 'def', 'ghi' ] };

    And that tells you that you have a hash ($pdata) containing two keys, 'blue' and 'red', where 'blue' is a pointer to a hash containing two keys and two values, and 'red' is a pointer to an array containing 3 values.



      I will take a closer look at perldata in a little bit. I have already looked into Data::Dumper a little bit. I do have to figure out how to write my HoHoA, but I will get there, some day, hopefully, soon.

      Thank you for stopping by and have a wonderful day!

      Lady Aleena

      "An it harm none, do as ye will."

      Data::Dumper is certainly the single most useful Perl module.

      The same module won't be required in OO languages, as long as something like tostring is defined at base object level, as many languages do.

      On the other hand, Perl does not require tostring, as all perl objects are hashes (or some other simple data structure). In the end, all Perl has is array and hash table. To that end, I like it. Let me say on the other hand one more time... on the other hand, to access a deep nested hash table or array can be very boring.

        on the other hand, to access a deep nested hash table or array can be very boring.
        You're joking, right? Nothing I like better than strapping my seat belt, puttin' on the safety goggles, and goin' zooming around those complex data structures, tight corners and all. But I'm not at all sure what that has to do with Perl-bashing.

        To address the rest of your post: Some say so. You're wrong. You're wrong. You're wrong. Whatever.

Re: A moment with Perl
by McDarren (Abbot) on Oct 07, 2007 at 04:28 UTC
    Howdy :)

    I endorse what my friend says about Data::Dumper. Extremely useful. Although, personally I like to use Data::Dumper::Simple - almost the same thing, but just a little easier to use and the output is slighly more user-friendly. Some people may recommend against this as it uses source-filtering, but my view is that for testing it is fine - I just make sure it never makes it into any production code.

    The other thing that you may find useful is the Perl Data Structures Cookbook (PDSC).

    And, if you are prepared to spend a few dollars, then Intermediate Perl is one of the best books (IMHO) for an introduction to references and data structures.

    Hope this helps,
    Darren :)


      I will look at Data::Dumper::Simple sometime soon though Data::Dumper isn't all that bad. I am not sure I understand what source-filtering is. There is a lot that I will have to remember to take out my my code when the scripts are done. The PDSC looks interesting, I will look into that more closely. As for buy that book, I think I need to get a beginners' book first, when we can afford it, instead of an intermediate one. When things are better, and the next time I am in the area of a bookstore, I will look into buying one.

      Have a wonderful day...

      Lady Aleena

      "An it harm none, do as ye will."

Re: A moment with Perl
by Zaxo (Archbishop) on Oct 07, 2007 at 05:39 UTC

    You're describing a deep data structure. There is organization implied by that, and I think that you may benefit from an OO approach that modularizes data access. If a chunk of data appears to be similar to all the others at its level, it is an object and deserves a module.

    After Compline,


      Could you possibly point me to some OO examples? I would be very interested in looking them over to see if they apply to what I was working on above.

      Have a gorgeous day....

      Lady Aleena

      "An it harm none, do as ye will."

Re: A moment with Perl
by Cop on Oct 07, 2007 at 01:40 UTC

    Most of your posts were around one thread - that random generator thread. People jumped in and provide you solutions. They are very nice.

    At the same time, what you really need to do is go to library borrow a Perl book, learn from the beginning. Don't buy books, I never do that.

    Since you are brand new to coding (obviously I don't consider HTML as coding), I suggest you start with something modern, I really recommend Python - it's easier than something like c# or java.

    Well, if you are maintaining some existing Perl stuff, then go ahead and learn Perl, but build a sound foundation, don't rely on full meal deals.


      Oh goodness, those who have been helping me with the random generator have been more than nice, they are wonderful. With each new solution, I learn more. Learning by doing is the best way for me to understand Perl.

      I will, more than likely, get a book sometime. Like bookstores, the nearest public library is in another town. Not too far away, but I hardly go to that town during the library's hours of operation.

      I am not sure that I have Python. Since I don't have ASP, PHP, or MySQL; I am pretty sure that Python is unavailable too. So, I will do my best with Perl and see just how far I can push it. I am hoping for more moments like the one above.

      Have a nice day...

      Lady Aleena

      "An it harm none, do as ye will."

        Aleena, it is a nice move in your career, and I wish you all the best.

        You cannot learn all those languages at once ;-) Go get Python, it is really easy to download and install. With Python, you don't even need to worry about MySQL initially, as Python comes with a database - SQLite. SQLite supports real SQL statements, and it should be good for you for a while.

        At a later point, move to MySQL, since 5.0, it is now a real database, and have almost everything you need.

        If you are not sure about the choice between Python and Perl, learn them side by side for 7 days, and I offer you all the help you need. make the right choice that is good for yourself in a long run. First and most importantly, Python is closer to all other modern languages, and will benefit you in a long run - if you ever need to learn another language, you will feel at home. This is not some sort of turf war, but as a responsible person, I say what a responsible person should say to IT newbies.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://643179]
Approved by liverpole
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-06-22 19:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.