Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi roadtest,

You have a number of issues, all of which are very minor.

For one thing, note that @owner and %owner are separate things (the first is an array, the second a hash), and you don't ever declare the hash:

my %owner = ( );

You should also (though you didn't ask about this) check the return value from open, otherwise (as I did) you could get a confusing error message like "readline() on closed filehandle FILE at C:\test\x.pl line 10".  Here's a simple fix for that:
open (FILE,"cron.log") or die "Can't open 'cron.log -- here's why: + $!\n";
but even better than that is to use the 3-arg open statement instead of the 2-arg version, which (along with lexical filehandles) is considered better programming practice:
use IO::File; my $fh = new IO::File("cron.log", "r") or die "Can't open 'cron.log' ( +$!)\n"; # Now use $fh in place of FILE ...

It might be better to break next if (split /[ ]+/)[1] !~ /oracle|sybase/ ; into multiple lines:

my @split = split /[ ]+/; my $split1 = $split[1] || ""; next if ($split1 !~ /oracle|sybase/)

so that when your input file doesn't contain what you expect, or something else goes wrong, you can debug the values in @split, or the value of $split1 (which might be undefined; hence my converting it to "").

You don't need to do (keys %{$owner} (which you've done in two or three places), it suffices to do (keys %owner) to get the keys of the hash.

Here's a version that runs without warnings:

#!/usr/bin/perl use strict; use warnings; use Date::Manip; use IO::File; my @owner = qw/oracle sybase/; my %owner = ( ); my $fh = new IO::File("cron.log") or die "Can't open 'cron.log' ($!)\n +"; while(<$fh>) { chomp; my @split = split /[ ]+/; my $split1 = $split[1] || ""; next if ($split1 !~ /oracle|sybase/); my ($mode,$owner,$job_id,undef,undef,undef,undef,$timestamp,undef) + = split /[ ]+/; #get same jobID finish timestamp and calculate difference, #then save back ${owner}{$job_id} = DateCalc (${$owner}{$job_id},$timestamp) if ($mode=~ /^>/); } close $fh; foreach my $owner(@owner) { foreach (keys %owner) { print "$owner - JobID:$_ - RunTime:${$owner}{$_}\n"; }; }

Now you can focus on whether the program is working as you wish...


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: hash array by liverpole
in thread hash array by roadtest

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found