3

I want to write the key and value pair that i have populated in the hash.I am using

open(OUTFILE,">>output_file.txt");
{
    foreach my $name(keys %HoH) {
        my $values = $HoH{$name};
        print "$name: $values\n";
    }
}
close(OUTFILE); 

Somehow it creates the output_file.txt but it does not write the data to it.What could be the reason?

Egga Hartung
  • 1,061
  • 11
  • 23
kunal
  • 107
  • 2
  • 2
  • 6
  • isn't the `">>output_file.txt"` syntax deprecated or something? http://perldoc.perl.org/functions/open.html – Tom Aug 05 '11 at 17:02
  • @Tom Not deprecated, just not a "best practice." – DavidO Aug 05 '11 at 17:20
  • @DavidO Some (including myself) could argue it belongs in the realm of "possibly bad practice, as it may bite you at some later date." Unfortunately, I don't think that's the official position. – kbenson Aug 05 '11 at 18:51
  • @kbenson I agree that 'bad practice' is a valid description. It's just not deprecated. – DavidO Aug 05 '11 at 21:24

4 Answers4

5

Use:

 print OUTFILE "$name: $values\n";

Without specifying the filehandle in the print statement, you are printing to STDOUT, which is by default the console.

sergio
  • 68,819
  • 11
  • 102
  • 123
4
open my $outfile, '>>', "output_file.txt";

print $outfile map { "$_: $HOH{$_}\n" } keys %HoH;

close($outfile);

I cleaned up for code, using the map function here would be more concise. Also I used my variables for the file handles, always good practice. There are still more ways to do this, you should check out Perl Cook book, here

wespiserA
  • 3,131
  • 5
  • 28
  • 36
1

When you open OUTFILE you have a couple of choices for how to write to it. One, you can specify the filehandle in your print statements, or two, you can select the filehandle and then print normally (without specifying a filehandle). You're doing neither. I'll demonstrate:

use strict;
use warnings;
use autodie;

my $filename = 'somefile.txt';

open my( $filehandle ), '>>', $filename;
foreach my $name ( keys %HoH ) {
    print $filehandle "$name: $HoH{$name}\n";
}
close $filehandle;

If you were to use select, you could do it this way:

use strict;
use warnings;
use autodie;

my $filename = 'somefile.txt';

open my( $filehandle ), '>>', $filename;
my $oldout = select $filehandle;
foreach my $name( keys %HoH ) {
    print "$name: $HoH{$name}\n";
}
close $filehandle;
select $oldout;

Each method has its uses, but more often than not, in the interest of writing clear and easy to read/maintain code, you use the first approach unless you have a real good reason.

Just remember, whenever you're printing to a file, specify the filehandle in your print statement.

DavidO
  • 13,812
  • 3
  • 38
  • 66
  • `open my $fh, ...` works just as well as `open my($fh), ...`. – Brad Gilbert Jan 03 '12 at 22:17
  • Not quite as well. When the "or die" clause is omitted, such as when autodie is in effect, if you've got warnings enabled, Perl may generate a warning if parens are omitted. Nevertheless, it DOES run correctly. See the bug report here: https://rt.perl.org/rt3/Public/Bug/Display.html?id=7250 and also on PerlMonks, here: http://www.perlmonks.org/?node_id=917108 It's possible this has already been resolved, but in 5.14 still seemed to persist. – DavidO Jan 10 '12 at 01:53
0

sergio's answer of specifying the filehandle is the best one.

Nonetheless there is another way: use select to change the default output filehandle. And in another alternate way to do things, using while ( each ) rather than foreach ( keys ) can be better in some cases (particularly, when the hash is tied to a file somehow and it would take a lot of memory to get all the keys at once).

open(OUTFILE,">>output_file.txt");
select OUTFILE;
while (my ($name, $value) = each %HoH) {
    print "$name: $value\n";
}
close(OUTFILE);
evil otto
  • 10,348
  • 25
  • 38