On 7 Jan 2004 11:43:09 -0800,
(Vumani Dlamini)
wrote:
>This problem follows up on a couple of problems I sent to the list 2
>months back. The data is structured as follows;
[snip]
>And the data set created is;
[snip]
>using the following Perl script;
>##### Perl script ######
>use strict;
>use warnings;
>open DATA, "c:/../properties.txt" or die "Unable to open file:$\n";
Probably not a very good idea calling it "DATA": no harm done, but you
may end up needing Perl's own DATA fh first or later...
>open PRIVATE, ">c:/.../private.txt";
aren't we checking here, eh?!?
[snip]
> elsif (/PROPemp=(\d+)/) {
> print PRIVATE "$Area$Comp$Pdes$Ppri$1\n";
This doesn't seem consistent with the "data set created" cut away from
the above paragraph.
Here's how I'd do it anyway:
#!/usr/bin/perl -l
use strict;
use warnings;
die "Usage: $0 <infile> <outfile>\n" unless @ARGV == 2;
my ($data,$priv);
open $data, '<', $_ or die "Unable to open `$_': $!\n" for shift;
open $priv, '>', $_ or die "Unable to open `$_': $!\n" for shift;
select $priv;
my %props;
while (<$data>) {
chomp;
warn("Input data mismatch"), next unless /^(\w+)=(\d+)\s*$/;
$props{$1}=$2;
if ($1 eq 'PROPemp') {
no warnings 'uninitialized';
local $,='|';
print map $props{$_},
qw/Area Company PROPdes PROPpri PROPemp/;
}
}
__END__
This is basically just as your own script. Only, IMHO, slightly more
perlish and more maintainable.
>I now have a "area text file" with specific companies that have to be
>extracted, with each row in the "area text file" having a code for an
>area. I would like to extract companies only in areas listed in the
>"area text file".
Oh, but then just add as the first statement of the 'if' block the
following line:
next unless in $props{'Area'}, @Areas;
Of course it is up to you to write a suitable 'in' sub (see a recent
thread on the subject too!) or substitute suitable code, and populate
@Areas. But that shouldn't be a problem...
Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__