The usual way to do this, is to first read the first file into a hash, basically creating a phone directory:
open my $fh, "<", "/root/Desktop/file1.txt"; my %verzeichnis; while (my $line = <$fh>) { $line =~ /RegEx/i; my $telefonnummer = $1; my $name = $2; $verzeichnis{lc $name} = $telefonnummer; } close $fh;
In a second loop (rather than a nested loop) afterwards, you would extract the name from each line, do a lookup in the hash and then write the result to a new file. At the end you can then replace your second file with the new file.
open my $fh2, "<", "/root/Desktop/file2.txt"; open my $new, ">", "/root/Desktop/file2.new"; while (my $line2 = <$fh2>) { chomp $line2; $line2 =~ /RegEx/i; my $name2 = "$1,$2"; print $new $line2; if( defined $verzeichnis{lc $name2} ) { print $new ";+49".$verzeichnis{lc $name2}; } print $new "\n"; } close $fh2; close $new;
Updated a few of typos.
In reply to Re^2: Write in files
by hdb
in thread Write in files
by lolz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |