Newbie needs some help

Discussion in 'Perl Misc' started by Kenneth D. Johnson, Oct 8, 2003.

  1. 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

    --
    Kenneth D. Johnson, Oct 8, 2003
    #1
    1. Advertising

  2. Kenneth D. Johnson

    Guest

    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 $!;
    , Oct 8, 2003
    #2
    1. Advertising

  3. Kenneth D. Johnson

    Helgi Briem Guest

    On Wed, 08 Oct 2003 04:29:33 GMT, "Kenneth D. Johnson"
    <> 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";
    Helgi Briem, Oct 8, 2003
    #3
  4. 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" <> wrote in message
    news:...
    > On Wed, 08 Oct 2003 04:29:33 GMT, "Kenneth D. Johnson"
    > <> 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";
    Kenneth D. Johnson, Oct 8, 2003
    #4
  5. Kenneth D. Johnson

    Helgi Briem Guest

    On Wed, 08 Oct 2003 13:47:22 GMT, "Kenneth D. Johnson"
    <> 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
    Helgi Briem, Oct 8, 2003
    #5
  6. "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
    John W. Krahn, Oct 8, 2003
    #6
  7. "Helgi Briem" <> 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
    LaDainian Tomlinson, Oct 9, 2003
    #7
  8. Kenneth D. Johnson

    Sam Holden Guest

    On Thu, 09 Oct 2003 03:29:46 GMT, LaDainian Tomlinson <> wrote:
    > "Helgi Briem" <> 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
    Sam Holden, Oct 9, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike

    Newbie needs some help

    Mike, May 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    281
    [MSFT]
    May 25, 2004
  2. Paul Smith

    ASP.Net - Newbie Needs Some Help

    Paul Smith, Feb 24, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    323
    Alan Silver
    Mar 3, 2005
  3. zcrow
    Replies:
    0
    Views:
    377
    zcrow
    Jun 12, 2005
  4. rh0dium
    Replies:
    1
    Views:
    316
    John Machin
    Jun 1, 2005
  5. Paul Smith

    ASP.Net - Newbie Needs Some Help

    Paul Smith, Feb 24, 2005, in forum: ASP .Net Web Controls
    Replies:
    9
    Views:
    164
    Alan Silver
    Mar 3, 2005
Loading...

Share This Page