Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Help needed with file output and hashes.

by Seventh (Beadle)
on Oct 31, 2005 at 18:54 UTC ( #504388=perlquestion: print w/replies, xml ) Need Help??
Seventh has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!

I have the following bit of code that I'm having a bit of trouble with. I'm in Iraq right now, so I apologize if a lot of this is something I can search/read about - internet time and books on Perl are in short supply around here.

Basically I have this script that opens a file called, and goes through it looking for a tag of "#CCR", then spits that, and the following line, to an html file.

It _kind of_ works, but I'm having two problems - one is that I get an error that says "Use of uninitialized hash element at line 48, <DATA> line (60, 80, 136)" when executing.

Secondly, it's ignoring my entire top section where I build the html header, and I don't know why. Any insight at all would really be appreciated, and thank you as always! :D

#!/usr/bin/perl -w use strict; use IO::File; use Data::Dumper; use Getopt::Long; open(DATA,"") or die $!; my $file ="ccrutil.html"; open FILE, ">$file" or die "unable to open $file $!"; print FILE "\<html\>\n"; # Build HTML header print FILE "\<title\>CCRUTIL.HTML\<\\title\>\n"; print FILE "\<body bgcolor\=\"black\"\>\n"; close FILE; my %functxt; while (<DATA>) { my $line = $_; chomp($line); if ($line =~ /^\#CCR/) { my ($functionName) = $line =~ /^\#CCR-(.*)/; my $desc = <DATA>; # pull the next line $functxt{$functionName} = $desc; } } foreach my $func (keys(%functxt)) { my $text = $functxt{$func}; my $file ="ccrutil.html"; open FILE, ">$file" or die "unable to open $file $!"; print FILE "\<b\>Function $func does $text\<\/b\>\n"; close FILE; }

Replies are listed 'Best First'.
Re: Help needed with file output and hashes.
by Zaxo (Archbishop) on Oct 31, 2005 at 19:04 UTC

    You don't need to open the DATA handle.

    Your main problem is that you reopen $file to truncate and write with '>'. Open to append with '>>' and your previous writings will survive. You should probably just open once and leave the file open until you're finished.

    After Compline,

      You don't need to open the DATA handle.

      Well, more to the point the filehandle DATA is special to perl (reading from it will give you what comes after the __END__ token in your source). It doesn't look like that's what you're doing so you might be better served (more to the point, the person who comes in down the road and tries to make sense of your code might be better served) using a different name for this handle (e.g. INFILE or DATAFILE).

Re: Help needed with file output and hashes.
by BrowserUk (Pope) on Oct 31, 2005 at 19:08 UTC

    The reason your header code is being ignored is because you are overwriting it. You open the file, write the header then close it, then later reopen the file using ">$file" which effectively erases it's contents, when you write the rest of you content. Use ">>$file" instead.

    For the uninitialised error, I can only guess that you have lines in your input file that contain '#CCR-' that are not followed by anything. Hence you regex fails to capture anything into $functionName and your attempt to use it as a hash key in the line  $functxt{$functionName} = $desc;, results in the warning. An inspection of the input file, or testing for and warning of (and perhaps skipping), lines that do not result in a capture would be the surest way to test that hypothesis.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Help needed with file output and hashes.
by pg (Canon) on Oct 31, 2005 at 19:09 UTC

    It is always nice to handle exceptions properly in your code. Modify your code a little bit:

    if (defined($functionName)) { my $desc = <DATA>; # pull the next line $functxt{$functionName} = $desc; } else { print STDERR "bad data: and attach some useful info for yo +ur debug purpose, such like the original data\n"; }
Re: Help needed with file output and hashes.
by jeffa (Bishop) on Oct 31, 2005 at 19:02 UTC

    First, we would really need to see some of the data to see why you are getting the Unitialized warning. Second, you are clobbering over your file for every loop pass in the bottom foreach. You already opened the file for writing at the start of the script, there is no need to re-open it in the foreach loop. This is why you are losing your HTML header info.

    Remove first line that closes your output file and move it to the end of the script. Likewise, remove the open and close lines in the foreach loop.


    (the triplet paradiddle with high-hat)
Re: Help needed with file output and hashes.
by Roy Johnson (Monsignor) on Oct 31, 2005 at 19:04 UTC
    Is there a line with CCR but no trailing -? You should really just do the pattern match once:
    if ($line =~ /^\#CCR-(.*)/) { my $functionName = $1; my $desc = <DATA>; # pull the next line $functxt{$functionName} = $desc; }
    What's happening is the assignment-match isn't happening, so $functionName is undef.

    Be careful over there.

    Caution: Contents may have been coded under pressure.
Re: Help needed with file output and hashes.
by graff (Chancellor) on Nov 01, 2005 at 05:39 UTC
    I think the others have taken care of the basic problems in your code. Just one more little nit-pick about how you present the problem...

    You quote a warning message that cites line 48 in your code, but the code you posted has less than 35 lines total. So we have to assume that there is some code you haven't shown us, and that by itself is not a bad thing -- you've posted the relevant parts, which is good.

    It would have helped to add a comment on the line that had originally been line #48 so that it's obvious where the error/warning message came from, and maybe even add a comment to say "# removed some irrelevant stuff from here...", just to eliminate any confusion about what you're showing us.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-05-25 21:31 GMT
Find Nodes?
    Voting Booth?