For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > Code Comments/Tips - Can this code be better?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Code Comments/Tips - Can this code be better?
Yitzle

2007-04-25, 6:58 pm

I got an input source that got records of fixed number of lines, eg
Name, Address, Age, Phone, Cell
I'm not interested in Age or Cell.
I'm doing something along the lines of the following. Can I do better?

my @lines = qw/name address age phone cell end/;
my %process = {name=>1, address=>1, phone=>1, end=>1};
my $i = 0;
my $name;
my %hash;
while(<INPUT> ) {
$name = $_ if($lines[$i] eq 'name');
next unless($process{$lines[$i]});
$hash{$name}{$lines[$i]} = $_;
} continue {
$i = ($i++) % (#$lines + 1);
}

One part that really irks me is the hash for listing what lines get processed.
I suppose I can process everything and have a qw// list of what parts
of the hash I want to use at a later point of the code...

Thanks
John W. Krahn

2007-04-25, 9:58 pm

yitzle wrote:
> I got an input source that got records of fixed number of lines, eg
> Name, Address, Age, Phone, Cell


So you are saying that 'Name' appears on every fifth line and *always* on
every fifth line?

> I'm not interested in Age or Cell.
> I'm doing something along the lines of the following. Can I do better?
>
> my @lines = qw/name address age phone cell end/;
> my %process = {name=>1, address=>1, phone=>1, end=>1};
> my $i = 0;
> my $name;
> my %hash;
> while(<INPUT> ) {
> $name = $_ if($lines[$i] eq 'name');
> next unless($process{$lines[$i]});
> $hash{$name}{$lines[$i]} = $_;
> } continue {
> $i = ($i++) % (#$lines + 1);


You should *never* use auto-incrment or auto-decrement on a variable that
appears more than once in an expression, *ever*!

( $i += 1 ) %= @lines;

> }
>
> One part that really irks me is the hash for listing what lines get
> processed.
> I suppose I can process everything and have a qw// list of what parts
> of the hash I want to use at a later point of the code...



my @lines = qw/ name address age phone cell end /;
my $name;
my %hash;

while ( <INPUT> ) {
chomp;
my $record = $. % @lines;
$name = $_ if $record == 1;
push @{ $hash{ $name } }, $_ if $record == 2 || $record == 4;
}



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Rob Dixon

2007-04-25, 9:58 pm

yitzle wrote:
>
> I got an input source that got records of fixed number of lines, eg
> Name, Address, Age, Phone, Cell
> I'm not interested in Age or Cell.
> I'm doing something along the lines of the following. Can I do better?
>
> my @lines = qw/name address age phone cell end/;
> my %process = {name=>1, address=>1, phone=>1, end=>1};
> my $i = 0;
> my $name;
> my %hash;
> while(<INPUT> ) {
> $name = $_ if($lines[$i] eq 'name');
> next unless($process{$lines[$i]});
> $hash{$name}{$lines[$i]} = $_;
> } continue {
> $i = ($i++) % (#$lines + 1);
> }
>
> One part that really irks me is the hash for listing what lines get
> processed.
> I suppose I can process everything and have a qw// list of what parts
> of the hash I want to use at a later point of the code...


I would put all of the data into the hash for completeness, and I would do
it like this:

use strict;
use warnings;

my @lines = qw/name address age phone cell end/;

my %hash;

my @data;

while (<INPUT> ) {

chomp;
push @data, $_;

if (@data == @lines) {

my %record;
@record{@lines} = @data;

$hash{$record{name}} = \%record;

undef @data;
}
}




Rob
Seanie

2007-04-25, 9:58 pm

yitzle wrote:
> I got an input source that got records of fixed number of lines, eg
> Name, Address, Age, Phone, Cell
> I'm not interested in Age or Cell.
> I'm doing something along the lines of the following. Can I do better?
>
> my @lines = qw/name address age phone cell end/;
> my %process = {name=>1, address=>1, phone=>1, end=>1};
> my $i = 0;
> my $name;
> my %hash;
> while(<INPUT> ) {
> $name = $_ if($lines[$i] eq 'name');
> next unless($process{$lines[$i]});
> $hash{$name}{$lines[$i]} = $_;
> } continue {
> $i = ($i++) % (#$lines + 1);
> }


This made my eyes bleed.

Does your input file have one record per line, or records in blocks separated
by blank lines, or what?
If you're trying to do what I think you're trying to do, it can be done in a
couple of lines of code, but please be specific about your objective here.
Like "I have this -->, and want this -->".

--
Seanie@wdcc.org.uk [pgp: 8A8FA6DE]

Yitzle

2007-04-26, 3:58 am

Sorry. The input file has one item per line. Line 1,6,11,etc has names #1,2,3,..
Line 2,7,12 has addresses #1,2,3...
I want to make a hash of hashed. hash{$name}{'address'} etc
I hope that's clear enough...

On 4/25/07, Seanie <seanie@wdcc.org.uk> wrote:
> yitzle wrote:
>
> This made my eyes bleed.
>
> Does your input file have one record per line, or records in blocks separated
> by blank lines, or what?
> If you're trying to do what I think you're trying to do, it can be done in a
> couple of lines of code, but please be specific about your objective here.
> Like "I have this -->, and want this -->".
>
> --
> Seanie@wdcc.org.uk [pgp: 8A8FA6DE]
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com