Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Arrays of Hashes

by williamp (Pilgrim)
on Sep 23, 2003 at 18:59 UTC ( [id://293616]=perlquestion: print w/replies, xml ) Need Help??

williamp has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

Thank you all very much for such a great resource.
I was looking at http://www.perlmonks.com/index.pl?node_id=1976 "making an array of hashes".
I put used the code but did not get the answer I expected, "Chuck".

Here is the code;

#!/usr/bin/perl -w use strict; my @array = ( { 'name' => 'Chuck', 'job' => 'roustabout' }, { 'name' => 'CowboyNeal', 'job' => 'code monkey' }); push @array, { 'name' => 'vroom', 'job' => 'stacking blocks' }; print $array[0]->{name}; sleep 30
Please would somebody tell me why Chuck is not returned.

Thank You for any help.

Replies are listed 'Best First'.
Re: Arrays of Hashes
by davido (Cardinal) on Sep 23, 2003 at 19:04 UTC
    'Chuck' is returned. You're just not seeing it because it's sitting in the STDOUT buffer while the computer sleeps for 30 seconds, rather than being flushed to the screen. You need to put a "\n" at the end of your print statement.

    print $array[0]->{name}, "\n";

    That ought to do it.

    The issue really has nothing to do with arrays of hashes, and everything to do with output buffering.

    Without the "\n" you're not guaranteed of having the output buffer get flushed to the screen, even when the program ends, though most OS's will flush it upon termination.

    What is happening is 'Chuck' gets put into the output buffer, the program sleeps for 30 seconds, the program reaches its termination, the output buffer gets flushed to the screen, and poof, your Microsoft DOS emulation window disappears on you before you can blink. Adding the "\n" newline tells Perl to flush the terminal's buffer.

    Note, "\n" causes terminal-tied buffers to flush, but doesn't necessarily flush other types of file handles (such as ones tied to files on your hard drive or a network socket). If you want to force all your "pipes to be piping hot" (force them to flush with each print), set a non-zero value in the output-autoflush system special variable, $|. Be aware that this will have a performance impact on every file-related thing you do in your program. Buffers exist for a reason. Try to avoid setting the autoflush variable unless you really need it. In your specific case, all you needed was a newline.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Arrays of Hashes
by jasonk (Parson) on Sep 23, 2003 at 19:02 UTC

    It is, but because it doesn't have a newline after it, and you are then sleeping, you aren't seeing it until the 30 seconds expires. Try either adding a newline at the end of the print, or removing the sleep.


    We're not surrounded, we're in a target-rich environment!

      I was skimming past this, thinking: it's the sleep that's delaying things. Then I read:

      Try either adding a newline at the end of the print

      which I don't understand. I tested it, and of course it works, but... why does adding a newline cause the print statement to be released before the sleep? Then, it made me think: why on earth is the *subsequent* sleep delaying an earlier print? Can anyone explain this to ignorant monk?

        The result is getting buffered, in case you print more output which should be appended to the end of the line you already have, adding the newline tells the system that you are definately not going to be adding new information to the end of that line, so it doesn't have to buffer it anymore. I'm assuming you are running this in some manner that causes a window to pop up and display the output and then disappears when the program is finished, which would explain the sleep. If you run it from the command line you would see that the buffer also gets flushed when the program exits, so you would get your output eventually, just not until the sleep was finished.


        We're not surrounded, we're in a target-rich environment!
        The issue is that Perl, like most other programming languages buffers screen output by default then outputs it all at once under certain circumstances. Those circumstances in Perl are either printing out a newline character, or manually flushing out the buffer. There may be others, I'm but a novice. Anybody know other ways?

        Hope that helps!
        Ben
Re: Arrays of Hashes
by injunjoel (Priest) on Sep 23, 2003 at 23:49 UTC
    when getting into creating nested data-types like hashes of arrays containing hashes and on and on...
    I always check my data-structure with a dumpvalue sub. Since I mostly work with HTML output my sub may look different than what you will use... but the point is that when you are venturing into the realm of creating complex data-types you should have an easy way to check that you are in fact doing what you intended.
    My code is below.
    #!/usr/bin/perl use Dumpvalue; sub dump_ref { my $ref = shift; my $dumper = new Dumpvalue; print "Content-Type:text/html\n\n"; print "<pre>"; $dumper->dumpValues($ref); print "</pre>"; exit; }

    Dumpvalue should be already installed with perl so there is nothing to download.
    the function should be passed a reference to the data you want to investigate.

    so say we have a hash called orders with nested data in it.
    call dump_ref with a reference to %orders
    dump_ref(\%orders);
    Hope that helps anyone working with nested data-types.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://293616]
Approved by jdtoronto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-26 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found