Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Initialising a hash

Reply
Thread Tools

Initialising a hash

 
 
Dave Saville
Guest
Posts: n/a
 
      02-05-2014
I have a script that processes apache log files.

One of the items it needs is a "number to english" look up of HTTP
error codes. There are around 80 of the things.

Given "200 OK" etc. per line I can see three ways of doing it:

1) External file, read and parse into a hash with the number as the
key.
2) Ditto but have the codes internally under __DATA__
3) Actually init as a hash - "code" => "english".

1 is out. I was doing it but forgot all about it and one day deleted
the file on a "what;s that doing here?" thought.

I have changed to 2, but just wonder about the pros and cons of 2 vs 3
- if there are any.

TIA
--
Regards
Dave Saville
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      02-05-2014
"Dave Saville" <(E-Mail Removed)> writes:
> I have a script that processes apache log files.
>
> One of the items it needs is a "number to english" look up of HTTP
> error codes. There are around 80 of the things.
>
> Given "200 OK" etc. per line I can see three ways of doing it:
>
> 1) External file, read and parse into a hash with the number as the
> key.
> 2) Ditto but have the codes internally under __DATA__
> 3) Actually init as a hash - "code" => "english".


Considering that RFC2616 defines 41 response codes, I think I'd just
grap them from the table of contents, use a perl one-liner to munge that
into 'hash syntax',

perl -ane '$F[0] =~ /(\d+\.){2}\d+/ or next; $e = $F[2]; $_ =~ /\./ and last or $e .= " $_" for @F[3 .. $#F]; print($F[1], "=> '\''$e'\'',\n");'

(works for a shell with 'bourne-style quoting rules')

and include that literally in the code (as 'body' of a hash
initialization, obviously).

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      02-05-2014
"Dave Saville" <(E-Mail Removed)> writes:

> I have a script that processes apache log files.
>
> One of the items it needs is a "number to english" look up of HTTP
> error codes. There are around 80 of the things.
>
> Given "200 OK" etc. per line I can see three ways of doing it:
>
> 1) External file, read and parse into a hash with the number as the
> key.
> 2) Ditto but have the codes internally under __DATA__
> 3) Actually init as a hash - "code" => "english".


4)

use HTTP::Status;

print status_message( $code ), "\n";



--
John Bokma j3b

Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
Perl for books: http://johnbokma.com/perl/help-in-ex...for-books.html
 
Reply With Quote
 
Huge
Guest
Posts: n/a
 
      02-05-2014
On 2014-02-05, Dave Saville <(E-Mail Removed)> wrote:
> I have a script that processes apache log files.
>
> One of the items it needs is a "number to english" look up of HTTP
> error codes. There are around 80 of the things.
>
> Given "200 OK" etc. per line I can see three ways of doing it:
>
> 1) External file, read and parse into a hash with the number as the
> key.
> 2) Ditto but have the codes internally under __DATA__
> 3) Actually init as a hash - "code" => "english".
>
> 1 is out. I was doing it but forgot all about it and one day deleted
> the file on a "what;s that doing here?" thought.


Which is why I always code the ability to have comments into such files
and comment them "HTTP Error codes, used by /usr/local/bin/apache_log_report",
or whatever.


--
Today is Sweetmorn, the 36th day of Chaos in the YOLD 3180
"Mistake Not My Current State Of Joshing Gentle Peevishness For The
Awesome And Terrible Majesty Of The Towering Seas Of Ire That Are
Themselves The Milquetoast Shallows Fringing My Vast Oceans Of Wrath"
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      02-06-2014
In article <(E-Mail Removed)>, Huge
<(E-Mail Removed)> wrote:

> On 2014-02-05, Dave Saville <(E-Mail Removed)> wrote:
> > I have a script that processes apache log files.
> >
> > One of the items it needs is a "number to english" look up of HTTP
> > error codes. There are around 80 of the things.
> >
> > Given "200 OK" etc. per line I can see three ways of doing it:
> >
> > 1) External file, read and parse into a hash with the number as the
> > key.
> > 2) Ditto but have the codes internally under __DATA__
> > 3) Actually init as a hash - "code" => "english".
> >
> > 1 is out. I was doing it but forgot all about it and one day deleted
> > the file on a "what;s that doing here?" thought.

>
> Which is why I always code the ability to have comments into such files
> and comment them "HTTP Error codes, used by /usr/local/bin/apache_log_report",
> or whatever.


Which is why I always create either a backup system or a configuration
control system, but usually both.

--
Jim Gibson
 
Reply With Quote
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      02-06-2014
On 02/05/14 10:38, Dave Saville wrote:
> I have a script that processes apache log files.
>
> One of the items it needs is a "number to english" look up of HTTP
> error codes. There are around 80 of the things.
>
> Given "200 OK" etc. per line I can see three ways of doing it:
>
> 1) External file, read and parse into a hash with the number as the
> key.
> 2) Ditto but have the codes internally under __DATA__
> 3) Actually init as a hash - "code" => "english".
>
> 1 is out. I was doing it but forgot all about it and one day deleted
> the file on a "what;s that doing here?" thought.
>
> I have changed to 2, but just wonder about the pros and cons of 2 vs 3
> - if there are any.


The main trade off for 2 is that you might someday decide you need your
__DATA__ section for something less amenable to a 3-ish solution than
this is.

Also, I suppose you might one day see the __DATA__ section and say
"What's that doing here" and delete it. If you are prone to doing that
sort of thing.

Xho
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      02-06-2014
On Wednesday, February 5, 2014 10:38:32 AM UTC-8, Dave Saville wrote:
> I have a script that processes apache log files.
> One of the items it needs is a "number to english" look up of HTTP
> error codes. There are around 80 of the things.
> Given "200 OK" etc. per line I can see three ways of doing it:
>
> 1) External file, read and parse into a hash with the number as the
> key.
> 2) Ditto but have the codes internally under __DATA__
> 3) Actually init as a hash - "code" => "english".
>
> 1 is out. I was doing it but forgot all about it and one day deleted
> I have changed to 2, but just wonder about the pros and cons of 2 vs 3
>


I'd combine them:

my %CODE;
{ local($/,$_); $_=<DATA>; %CODE = /(...)...(...)/mg }


--
Charles DeRykus
 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      02-06-2014
On Thu, 6 Feb 2014 00:47:09 UTC, Jim Gibson <(E-Mail Removed)>
wrote:

> In article <(E-Mail Removed)>, Huge
> <(E-Mail Removed)> wrote:
>


<snip>

> > Which is why I always code the ability to have comments into such files
> > and comment them "HTTP Error codes, used by /usr/local/bin/apache_log_report",
> > or whatever.


Unfortunately I did not look in the file - I just went by the name.


>
> Which is why I always create either a backup system or a configuration
> control system, but usually both.
>


I do have a backup - but the deletion was over a year ago and I don't
keep them that long.

--
Regards
Dave Saville
 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      02-06-2014
On Wed, 5 Feb 2014 21:02:03 UTC, John Bokma <(E-Mail Removed)>
wrote:

> "Dave Saville" <(E-Mail Removed)> writes:
>
> > I have a script that processes apache log files.
> >
> > One of the items it needs is a "number to english" look up of HTTP
> > error codes. There are around 80 of the things.
> >
> > Given "200 OK" etc. per line I can see three ways of doing it:
> >
> > 1) External file, read and parse into a hash with the number as the
> > key.
> > 2) Ditto but have the codes internally under __DATA__
> > 3) Actually init as a hash - "code" => "english".

>
> 4)
>
> use HTTP::Status;
>
> print status_message( $code ), "\n";


Thanks for that - Did not know of it - and I had it Specific
solution but I was also after the general case. Which others have
commented on.
--
Regards
Dave Saville
 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      02-06-2014
On Thu, 6 Feb 2014 04:08:35 UTC, Xho Jingleheimerschmidt
<(E-Mail Removed)> wrote:

<snip>

>
> Also, I suppose you might one day see the __DATA__ section and say
> "What's that doing here" and delete it. If you are prone to doing that
> sort of thing.


LOL. Much, much less likely to delete a chunk of a script without too
much thought than a data file that appeared in a cgi directory.
Actually the decision was slightly more complicated than "Why's that
here? rm" but the details are irrelevant to the discussion in hand.
--
Regards
Dave Saville
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Re: Dynamic user control loading/initialising Phil Winstanley [Microsoft MVP ASP.NET] ASP .Net 2 06-20-2004 01:26 PM
Dynamic user control loading/initialising Jo Inferis ASP .Net 0 06-20-2004 12:51 PM
Initialising a signal jan VHDL 3 12-23-2003 01:25 PM



Advertisments