List Info

Thread: Re: repeated use of grep




Re: repeated use of grep
country flaguser name
United States
2007-06-11 09:38:35

John,

> Note that if there's only two pieces of information per package in
> the PACKAGES.TXT file, you might want to use a hash instead of the
> two arrays.

Thanks for the reply. I found myself thinking with this step of the
project that since the lines I wanted went name, location, name,
location and so on, that it would be a good candidate for pulling into
a hash: they're pairs. And then I thought, well, would a more
complicated structure be more efficient/relevant, since there are many
files under each location? Something like a hash of array references?
And that's where I started to get tired and kinda lost.

So I stepped back and tried to go more slowly, step by step ... and
got MORE lost. :^D

Regards,

G

__._,_.___
.

__,_._,___
Re: repeated use of grep
country flaguser name
United States
2007-06-11 20:21:29

Below is what I came up with to turn the relevant lines of
PACKAGES.TXT into a hash. It appears to work, tho I am confident there
is a (are many!) way(s) to do this more elegantly and -- maybe in one
step instead of two ...

Glenn

---------------------

#!/usr/bin/perl

use warnings;
use strict;

my (packages, locations, %mirror);

open FH, "PACKAGES.TXT"
or die "Can't open file: $!";

foreach (<FH>;) {
if (/^PACKAGE NA/) {
/:s+(.*tgz$)/;
push packages, $1;
} elsif (/^PACKAGE LO/) {
/:s+(./.*$)/;
push locations, $1;
}
}

close FH;

for (0..$#packages) {
$mirror{$packages[$_]} = $locations[$_]
}

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )