joseph wrote:
>
> I need help with my script, this supposedly would check a workstation listed
> in one file then see if it's not listed on other file, if it 's the case
> then append it on an output file.Basically what i got is the same unmodified
> list just appended on the output file. Where did i got  wrong? TIA.
>
> #!/usr/bin/perl -w
>
> use strict;
> use Data::Dumper;
>
> open(FL1,"pclist.txt") or die "can't open pclist.txt $!";
> open(FL2,"smsclient.txt") or die "can't open smsclient.txt $!";
> open(OUTPUT,">>unlisted.txt") or die "can't open pclist.txt $!";
>
> my @smsclient = <FL1>;
> my @pclist = <FL2>;
> my %hash = map { $_ => 1 } @pclist;
>
> foreach my $key (@smsclient) {
>    chomp($key);
>    unless(exists $hash{$key}){
>    print OUTPUT $key,"\n";
>     }
>   }
>
>  close(FL1);
>  close(FL2);
>  close(OUTPUT);

Hi Joseph

You've used the raw file records in @pclist as keys for your hash, but then
'chomp'ed the data in @smsclient before you look for a match. Nothing will
compare as equal because one set has trailing "\n" characters while the other
doesn't.

By the way, you're putting the stuff from pclist.txt into @smsclient and vice
versa, which could confuse things a little.

You may like my version of this program, which works fine with my data. One
proviso though: I don't know which of your files is which, so you may need to
swap the filenames. This prints all names in smsclient.txt that aren't in
pclist.txt.

HTH,

Rob

  use strict;
  use warnings;

  my %pclist = do {
    open my $fh, 'pclist.txt' or die $!;
    chomp (my @data = <$fh>);
    map { $_ => 1 } @data;
  };

  open my $fh, 'smsclient.txt' or die $!;
  open my $out, '>>', 'unlisted.txt' or die $!;

  while (<$fh>) {
    chomp;
    print $out "$_\n" unless $pclist{$_};
  }

  close $out;
  close $fh;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to