1,105,169 Community Members

Perl write in a text file if no duplicate

Member Avatar
ghosh22
Junior Poster in Training
65 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi everyone..
I am trying to write a perl code which appends a text file after taking two inputs from the user. It saves the file in tab delimited format. I have the input text file attached here. Now I want the file to check if the user given input is already present only in the first column of the text file. If it is present, then it doesn't append anything in the file. Otherwise it should append it.
Below is my code which is just comparing..not appending!

print "Please write your name and address below\n";
$input=<STDIN>;
$input1=<STDIN>;

chomp $input;
chomp $input1;

open FILE, "C:/Software/new1.txt" or die $!;

while (<FILE>) {
 chomp;
  ($user)=split("\s");
    if ($input=~/$user/) {
      print "User name already exists.\n";
      #close (FILE);
      }
    else {
    #open FILE, ">>C:/Software/new1.txt" or die $!;
     print FILE "$input\t$input1";
     }
  }
close (FILE);
exit;

could you please help me..
Thanks

Attachments new1.txt (0.06KB)
Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

The Tie::File module lets you treat a file like an array so you can repeatedly loop through it and push new records into it.

#!/usr/bin/perl
#tie_file.pl;
use strict;
use warnings;

use Tie::File;

my $filename = '/home/david/Programming/data/new1.txt';

tie my @array, 'Tie::File', $filename or die "Unable to Tie $filename: $!";

my ($input, $input1);

($input, $input1) = ('David', 'at home');#Try adding myself

if ( already_in_file($input) ){
    print "User name already exists.\n";
}
else{
    push @array, "$input\t$input1";
    print "Added $input to $filename.\n"
}

sub already_in_file{
    my $u2find = shift;
    my $user;
    
    foreach (@array){
        ($user) = split /\t/;
        if ($user =~ m/$u2find/i){
            return 1;#True if user already in file
        }
    }
    return 0;#False if user not found in file
}
Member Avatar
ghosh22
Junior Poster in Training
65 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HI David,
Many thanks for your code. its working perfectly!!

Member Avatar
ghosh22
Junior Poster in Training
65 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

one more thing..I am newbie in perl so trying to learn..does return 1 always denotes true and return 0 always returns false..and is it mandatory to use them in subroutines? Also many perl books says that while can be used for multi line file looping. But in my case it did not work. Then can we say, while loop is not useful always?
thanks

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

one more thing..I am newbie in perl so trying to learn..does return 1 always denotes true and return 0 always returns false..and is it mandatory to use them in subroutines? Also many perl books says that while can be used for multi line file looping. But in my case it did not work. Then can we say, while loop is not useful always?
thanks

My subroutine returns 1 or 0 because I want to evaluate the subroutine in a boolean context. In the above script if ( already_in_file($input) ){... calls the subroutine and evaluates it in a boolean context in which undef, zero, empty string and empty list evaluate to false and anything else evaluates to true.
Truth and Falsehood...The number 0, the strings '0' and '' , the empty list () , and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as '' , but as a number, it is treated as 0.

It is not mandatory for a subroutine to return 1 or 0. You could have a subroutine return any value you want.

Sure you can use a while loop to loop through a multi-line file, but in the above script you wanted to loop through and sometimes add to the file, so using Tie::File lets you treat the file like an array. I find in this case that a foreach loop is easier for looping through an array. I don't know any written rule for when to use while loops versus when to use foreach loops. Use what works and makes sense to you and is readable enough for other programmers to understand.

Member Avatar
ghosh22
Junior Poster in Training
65 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks for your nice reply..marked solved

Question Answered as of 2 Years Ago by d5e5
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article