#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    509
    Rep Power
    49

    Writing to a file


    I want to append a data entry to a file that is comprised of a single column filled with rows or lines of single name text data, if that entry doesn't already exist in the file.

    I've written this:
    $mydata = "my name";
    open(LOG, ">>$myfile");
    @entries = <LOG>;
    close(LOG);
    $entry_exists = 0;
    foreach $entry (@entries) {
    chomp $entry;
    if ($entry eq $mydata) {
    $entry_exists = 1;
    }
    }
    if ($entry_exists = 0) {
    open(LOG, ">>$myfile");
    print LOG "$this\n";
    close(LOG);
    }


    I haven't tested this yet, but is this an efficient way of doing this? is there a defacto standard or is there a better way to accomplish the goal with less processor cycles....

    Thanks
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,878
    Rep Power
    3890
    Code:
    if ($entry_exists = 0) {
    Note that this will always be false. A single equals sign causes 0 to be assigned as the value of $entry_exists, which is what is being evalueted by the `if' statement to see if it's true, which it never is (since zero is a false value in Perl). You should be using the `==' operator instead, to test for numeric equality.

    Code:
    open(LOG, ">>$myfile");
    This will open the file in `append' mode (i.e. add anything you print to the file on to the end of it). This is what you need the second time, but not the first. The first time, you need to open the file in `read' mode, which would be either of the following:
    Code:
    open(LOG, "$myfile");
    open(LOG, "<$myfile");
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    509
    Rep Power
    49
    Thanks. I oversighted the open LOG to read...I knew it didn't get ">>"

    I'll make the change to == and now I understand why not just one = I always kind of wondered about that.

    Now I have a question with opening an empty file that contains no data, checking it and writing $mydata to it. Such as:

    $mydata = "my name";
    open(LOG, "$myfile");
    @entries = <LOG>;
    close(LOG);
    foreach $entry (@entries) {
    chomp $entry;
    if ($entry ne $mydata) {
    open(LOG, ">>$myfile");
    print LOG "$mydata\n";
    close(LOG);
    }
    }
    What I see happening is that foreach see's nothing and therefore it seems to abort. Is that supposed to be that way?

    If the file is empty nothing is written, but if the file has some data in it then $mydata will be written to it.

    Thanks.
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,878
    Rep Power
    3890
    If there's nothing in the file, @entries will be empty and the foreach loop will not start.

    You've changed the logic of your program now. This way won't work as you expect.

    With this new version, $mydata will be added to the file every time it's compared with a string that isn't the same. That means that if you have a file with 5 entries in it, one of which is the same as $mydata, it will cause 4 new lines to be added to the file, since it was compared with 4 strings that weren't the same.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    509
    Rep Power
    49
    Thanks for your help.

    I've made the corrections and have it running great.

    Comments on this post

    • ishnid agrees : You're welcome. Good to hear it.

IMN logo majestic logo threadwatch logo seochat tools logo