wrote:
> ok ... I changed it .. but when I run my new script it prints adresses
> more than once .. why is that?
because you print the keys inside the loop
> Here's my code:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $textfile = 'empfaenger.txt';
>
> open(EMPFAENGER, $textfile) || die("Could not open file $textfile");
open my $fh, $textfile or die "Can't open '$textfile': $!";
$! = why it didn't work, if you don't print that, you get quite a
meaningless error
> my @raw_data = <EMPFAENGER>;
if you do this, you can close now, not after the loop, or:
my %ad_hash;
while ( my $line = <$fh> ) {
> my %ad_hash;
>
> foreach my $line (@raw_data)
> {
> my @fields = split(/ /, $line);
> my @fields2 = split(/=/, $fields[6]);
> my $address = $fields2[1];
> $address = substr($address,1,length($address) - 3);
this probably could be done in a shorter way
> if(index($address,"domain.ch") > 0)
> {
> $ad_hash{$address} = 1;
> }
$ad_hash{ $address } = 1 if index( $address, "domain.ch") > 0;
}
or:
index( $address, "domain.ch" ) > 0 and $ad_hash{ $address } = 1;
}
or:
index( $address, "domain.ch" ) > 0 or next;
$ad_hash{ $address } = 1;
}
then close:
close $fh or die "Can't close '$textfile': $!";
> foreach my $key(keys(%ad_hash))
> {
> print $key . "\n";
> }
You can write the print as:
print "$key\n";
a shorter way to write the print all:
print "$_\n" for keys %add_hash;
or
print map { "$_\n } keys %add_hash;
--
John Small Perl scripts:
http://johnbokma.com/perl/
Perl programmer available:
http://castleamber.com/
Happy Customers:
http://castleamber.com/testimonials.html