Professional Web Applications Themes

Question about OUTFILE? - PERL Beginners

Hey all, I'm trying to read from a file, copy it's contents to another file, then overwrite the first file. >open (INFILE, "<myfirstfile.txt") || die "Error opening myfirstfile.$!,stopped"; >array1 = <INFILE>; >close(INFILE); >foreach my $rec (array1) { chomp($rec); ($x,$y,$z) = split(/\|/,$rec); if ($y ne "AK") { #this leaves one record uncopied. open (OUTFILE, ">>mysecondfile.txt") || die "Error opening mysecondfile.$!,stopped"; print OUTFILE "$rec\n"; close(OUTFILE); }; >}; Normally I should just overwrite the first file with an "open", right? >open (OUTFILE, ">myfirstfile.txt") || die "Error opening myfirstfile.$!,stopped"; >print OUTFILE "$a|$b|$c\n"; >close(OUTFILE); Then I can copy the other stuff back replacing the original ...

  1. #1

    Default Question about OUTFILE?

    Hey all,
    I'm trying to read from a file, copy it's contents to another file,
    then overwrite the first file.
    >open (INFILE, "<myfirstfile.txt") || die "Error opening
    myfirstfile.$!,stopped";
    >array1 = <INFILE>;
    >close(INFILE);
    >foreach my $rec (array1) {
    chomp($rec);
    ($x,$y,$z) = split(/\|/,$rec);
    if ($y ne "AK") { #this leaves one
    record uncopied.
    open (OUTFILE, ">>mysecondfile.txt") || die "Error
    opening mysecondfile.$!,stopped";
    print OUTFILE "$rec\n";
    close(OUTFILE);
    };
    >};
    Normally I should just overwrite the first file with an "open", right?
    >open (OUTFILE, ">myfirstfile.txt") || die "Error opening
    myfirstfile.$!,stopped";
    >print OUTFILE "$a|$b|$c\n";
    >close(OUTFILE);
    Then I can copy the other stuff back replacing the original record with
    the new one I just pasted in there.
    >open (INFILE, "<mysecondfile.txt") || die "Error opening
    mysecondfile.$!,stopped";
    >array2 = <INFILE>;
    >close(INFILE);
    >foreach my $rec (array2) {
    chomp($rec);
    open (OUTFILE, ">>myfirstfile.txt") || die "Error opening
    myfirstfile.$!,stopped";
    print OUTFILE "$rec\n";
    close(OUTFILE);
    };
    However, I seem to get doubles of the record left behind, the old record
    does not seem to go away, what am I doing wrong?

    Sincerely in Christ,
    Mark-Nathaniel Weisman
    President / Owner
    Outland Domain Group
    Anchorage, Alaska



    Mark Weisman Guest

  2. #2

    Default Re: Question about OUTFILE?

    Mark Weisman wrote:
    >
    > I'm trying to read from a file, copy it's contents to another file,
    > then overwrite the first file.
    >
    > open (INFILE, "<myfirstfile.txt") || die "Error opening myfirstfile.$!,stopped";
    > array1 = <INFILE>;
    > close(INFILE);
    >
    > foreach my $rec (array1) {
    > chomp($rec);
    > ($x,$y,$z) = split(/\|/,$rec);
    > if ($y ne "AK") { #this leaves one record uncopied.
    > open (OUTFILE, ">>mysecondfile.txt") || die "Error opening mysecondfile.$!,stopped";
    > print OUTFILE "$rec\n";
    > close(OUTFILE);
    > };
    > };
    >
    > Normally I should just overwrite the first file with an "open", right?
    >
    > open (OUTFILE, ">myfirstfile.txt") || die "Error opening myfirstfile.$!,stopped";
    > print OUTFILE "$a|$b|$c\n";
    > close(OUTFILE);
    >
    > Then I can copy the other stuff back replacing the original record with
    > the new one I just pasted in there.
    >
    > open (INFILE, "<mysecondfile.txt") || die "Error opening mysecondfile.$!,stopped";
    > array2 = <INFILE>;
    > close(INFILE);
    >
    > foreach my $rec (array2) {
    > chomp($rec);
    > open (OUTFILE, ">>myfirstfile.txt") || die "Error opening myfirstfile.$!,stopped";
    > print OUTFILE "$rec\n";
    > close(OUTFILE);
    > };
    >
    > However, I seem to get doubles of the record left behind, the old record
    > does not seem to go away, what am I doing wrong?
    Hi Mark.

    Well you're doing several things wrongly:

    - I had to reformat your code before I could understand what it was doing!
    - Always 'use strict' and 'use warnings'
    - Opening and closing a file to add each record is just wasteful
    - There's no need to remove the old record and add the new one in two steps

    But I think the answer to your question is that you're not identifying the
    record to be deleted properly. You're looking for a pipe-separated record
    with 'AK' in the second field, is that right? It must also have no leading or
    trailing whitespace. If you just put a 'print' statement in your conditional
    then you'll be able to see which records are being rejected, like this:

    if ($y eq "AK") { #this leaves one record uncopied.
    print "Record rejected: $rec\n";
    }
    else {
    open (OUTFILE, ">>mysecondfile.txt") || die "Error opening mysecondfile.$!,stopped";
    print OUTFILE "$rec\n";
    close(OUTFILE);
    }

    Take a look at my program below which should do what you want. The scalar
    variables $a, $b and $c are undeclared, but apart from that it should
    compile.

    HTH,

    Rob



    use strict;
    use warnings;

    open INFILE, "myfile.txt" or die $!;
    my array = <INFILE>;
    close INFILE;

    open OUTFILE, "> myfile.txt" or die $!;
    print OUTFILE "$a|$b|$c\n";

    foreach (array) {
    my (undef, $field2) = split /\|/;
    print OUTFILE $_ unless $field2 eq 'AK';
    }

    close OUTFILE;


    Rob Dixon Guest

Similar Threads

  1. Newbie Question: Biz Card Template Question
    By Thomas_Porter@adobeforums.com in forum Adobe Indesign Windows
    Replies: 4
    Last Post: May 30th, 08:08 AM
  2. Replies: 3
    Last Post: August 17th, 02:20 PM
  3. Replies: 9
    Last Post: April 27th, 04:44 AM
  4. Pen Tool Use Question. (Embarrassingly Newbie Question)
    By Bozo Schmozo in forum Macromedia Flash
    Replies: 0
    Last Post: November 12th, 10:00 PM
  5. newB question: related tables question
    By Blue man in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: June 30th, 04:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139