Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie needs some help

Reply
Thread Tools

Newbie needs some help

 
 
Kenneth D. Johnson
Guest
Posts: n/a
 
      10-08-2003
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

--



 
Reply With Quote
 
 
 
 
someone@example.com
Guest
Posts: n/a
 
      10-08-2003
On Wed, 08 Oct 2003 04:29:33 +0000, Kenneth D. Johnson wrote:

>
> 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 $!;


 
Reply With Quote
 
 
 
 
Helgi Briem
Guest
Posts: n/a
 
      10-08-2003
On Wed, 08 Oct 2003 04:29:33 GMT, "Kenneth D. Johnson"
<(E-Mail Removed)> wrote:

>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";
 
Reply With Quote
 
Kenneth D. Johnson
Guest
Posts: n/a
 
      10-08-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, 08 Oct 2003 04:29:33 GMT, "Kenneth D. Johnson"
> <(E-Mail Removed)> wrote:
>
> >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";



 
Reply With Quote
 
Helgi Briem
Guest
Posts: n/a
 
      10-08-2003
On Wed, 08 Oct 2003 13:47:22 GMT, "Kenneth D. Johnson"
<(E-Mail Removed)> wrote:

>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


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-08-2003
"Kenneth D. Johnson" wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
LaDainian Tomlinson
Guest
Posts: n/a
 
      10-09-2003
"Helgi Briem" <(E-Mail Removed)> wrote:

<snip>

> 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.
--
bclennox AT eos DOT ncsu DOT edu


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.522 / Virus Database: 320 - Release Date: 9/29/2003


 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      10-09-2003
On Thu, 09 Oct 2003 03:29:46 GMT, LaDainian Tomlinson <(E-Mail Removed)> wrote:
> "Helgi Briem" <(E-Mail Removed)> wrote:
>
><snip>
>
>> 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?


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.

--
Sam Holden

 
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
newbie needs some xhtml & xml parser help zcrow XML 0 06-12-2005 07:35 PM
ASP.Net - Newbie Needs Some Help Paul Smith ASP .Net Web Controls 9 03-03-2005 06:49 PM
ASP.Net - Newbie Needs Some Help Paul Smith ASP .Net 9 03-03-2005 06:49 PM
Newbie needs some help Mike ASP .Net 3 05-25-2004 05:27 AM
Some questions regarding 070-305 and hopefully some right answers. Needs correction... wink, wink ;-) Daniel Walzenbach MCSD 1 11-10-2003 12:25 AM



Advertisments