Newbie needs some help

  • Thread starter Kenneth D. Johnson
  • Start date
K

Kenneth D. Johnson

Here is what I'm trying to do:

I have file1 that has:
100
200
300
etc.

I have file2 that has
XXX XX XXXXXX XXXX XXXX [100] AAA
XXX XX XXXXXX XXXX XXXX [200] BBB
XXX XX XXXXXX XXXX XXXX [225] CCC
XXX XX XXXXXX XXXX XXXX [250] DDD
XXX XX XXXXXX XXXX XXXX [300] EEE
etc.

I want to find the lines in file2 that contain the numbers in file1 so I can
write to a third file the A, B, C, D, E info that follows the numbers in
file2.

I will sure appreciate any help.

Thanks!

KDJ

--
 
S

someone

I want to find the lines in file2 that contain the numbers in file1 so I can
write to a third file the A, B, C, D, E info that follows the numbers in
file2.

I will sure appreciate any help.

If you want help, then you should show what you've tried and failed at
doing. By supplying an answer, I am a screwing you out of the joy of
learning. Sorry.

I'd start here:
http://learn.perl.org/library/beginning_perl/


use strict;
use warnings;

open FILE1, "file1" or die $!;
open FILE2, "file2" or die $!;
open FILE3, "> file3" or die $!;
my %valid;

while (<FILE1>)
{
chomp;
$valid{$_} = 1;
}

close FILE1 or die $!;

while (<FILE2>)
{
if (/\[(\S+)\]\s+(.*)$/ and $valid{$1})
{ print FILE3 $2 . "\n" }
}

close FILE2 or die $!;
close FILE3 or die $!;
 
H

Helgi Briem

I have file1 that has:
100
200
300
etc.

I have file2 that has
XXX XX XXXXXX XXXX XXXX [100] AAA
XXX XX XXXXXX XXXX XXXX [200] BBB
XXX XX XXXXXX XXXX XXXX [225] CCC
XXX XX XXXXXX XXXX XXXX [250] DDD
XXX XX XXXXXX XXXX XXXX [300] EEE
etc.

I want to find the lines in file2 that contain the numbers in file1 so I can
write to a third file the A, B, C, D, E info that follows the numbers in
file2.

Here's one way:

#!perl
use warnings;
use strict;
my $usage = "Usage: $0 wanted infofile outfile\n";
die $usage if not $ARGV[2];
my ($wanted, $infofile, $outfile) = @ARGV;

my %wanted;
open WANTED, $wanted or die "Cannot open $wanted:$!\n";
while (<WANTED>)
{
next if (/^\s+$/); # ignore blank lines
chomp;
$wanted{$_}++;
}
close WANTED;

open OUT, ">", $outfile
or die "Cannot open $outfile for writing:$!\n";

open INFO, $infofile or die "Cannot open $infofile:$!\n";
{
while (<INFO>)
{
next if (/^\s+$/); # ignore blank lines
if (/\[(\d+)\](.+)/ and ($wanted{$1}))
{
print OUT $1,$2, "\n";
}
}
}
close INFO;
close OUT or die "Cannot close $outfile:$!\n";
 
K

Kenneth D. Johnson

Thanks to the several who helped and even to those who only have this forum
as an opportunity to show their genius. :) One learns from all.

KDJ

--
"We cannot have real learning in school if we think it is our duty and our
right to tell children what they must learn."
John Holt
How Children Fail (179)

Helgi Briem said:
I have file1 that has:
100
200
300
etc.

I have file2 that has
XXX XX XXXXXX XXXX XXXX [100] AAA
XXX XX XXXXXX XXXX XXXX [200] BBB
XXX XX XXXXXX XXXX XXXX [225] CCC
XXX XX XXXXXX XXXX XXXX [250] DDD
XXX XX XXXXXX XXXX XXXX [300] EEE
etc.

I want to find the lines in file2 that contain the numbers in file1 so I can
write to a third file the A, B, C, D, E info that follows the numbers in
file2.

Here's one way:

#!perl
use warnings;
use strict;
my $usage = "Usage: $0 wanted infofile outfile\n";
die $usage if not $ARGV[2];
my ($wanted, $infofile, $outfile) = @ARGV;

my %wanted;
open WANTED, $wanted or die "Cannot open $wanted:$!\n";
while (<WANTED>)
{
next if (/^\s+$/); # ignore blank lines
chomp;
$wanted{$_}++;
}
close WANTED;

open OUT, ">", $outfile
or die "Cannot open $outfile for writing:$!\n";

open INFO, $infofile or die "Cannot open $infofile:$!\n";
{
while (<INFO>)
{
next if (/^\s+$/); # ignore blank lines
if (/\[(\d+)\](.+)/ and ($wanted{$1}))
{
print OUT $1,$2, "\n";
}
}
}
close INFO;
close OUT or die "Cannot close $outfile:$!\n";
 
H

Helgi Briem

Thanks to the several who helped and even to those who only have this forum
as an opportunity to show their genius. :) One learns from all.

You thank me and then spit in my face by rudely top-posting
with a full copy. Don't do that. If you don't know what top-posting
is, read:

http://www.catb.org/~esr/jargon/html/T/top-post.html

For further guidelines on posting to comp.lang.perl.misc read:

http://mail.augustmail.com/~tadmc/clpmisc.shtml

For more information about netiquette in general, see the
"Netiquette Guidelines" at:

http://andrew2.andrew.cmu.edu/rfc/rfc1855.html
 
J

John W. Krahn

Kenneth D. Johnson said:
Here is what I'm trying to do:

I have file1 that has:
100
200
300
etc.

I have file2 that has
XXX XX XXXXXX XXXX XXXX [100] AAA
XXX XX XXXXXX XXXX XXXX [200] BBB
XXX XX XXXXXX XXXX XXXX [225] CCC
XXX XX XXXXXX XXXX XXXX [250] DDD
XXX XX XXXXXX XXXX XXXX [300] EEE
etc.

I want to find the lines in file2 that contain the numbers in file1 so I can
write to a third file the A, B, C, D, E info that follows the numbers in
file2.


#!/usr/bin/perl
use warnings;
use strict;

open my $fh, '<', 'file1' or die "Cannot open file1: $!";
my %find = map { $_ => 1 } do { local $/; <$fh> =~ /\d+/g };
close $fh;

open $fh, '<', 'file2' or die "Cannot open file2: $!";
open my $out, '>', 'file3' or die "Cannot open file3: $!";
while ( <$fh> ) {
if ( / \[ ( \d+ ) ] \s* ( .+ ) /x ) {
print $out "$2\n" if exists $find{ $1 };
}
}

__END__



John
 
L

LaDainian Tomlinson

open WANTED, $wanted or die "Cannot open $wanted:$!\n"; ....
close WANTED;
open OUT, ">", $outfile
or die "Cannot open $outfile for writing:$!\n"; ....
close OUT or die "Cannot close $outfile:$!\n";

open INFO, $infofile or die "Cannot open $infofile:$!\n"; ....
close INFO;

Can I ask why you chose only to check the return value of close for OUT? Is
it because you're writing to a file rather than only reading from it? I can
see why a hung write might keep the file from closing properly, but could a
hung read not do the same for the other files?

Thanks for any clarification,

Brandan L.
 
S

Sam Holden

Can I ask why you chose only to check the return value of close for OUT? Is
it because you're writing to a file rather than only reading from it? I can
see why a hung write might keep the file from closing properly, but could a
hung read not do the same for the other files?

A "hung read" will just cause the read to not return, and the script to
"freeze".

A failure to close a file open for reading is irrelevant from the point
of view of a script, and I can't think of a reason for such a failure to
occur (though there probably is, with something like NFS...).

A failure to close a file open for writing on the other hand is very
important. The data you have written to the file has possibly not been
written to the file after all. The disk could be full, for example.
Such data loss situations are important enough for a program to let
the user know in no uncertain terms that something has gone wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top