in reply to Re: Write in files
in thread Write in files
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Write in files
by lolz (Initiate) on Nov 18, 2013 at 10:47 UTC | |
by hdb (Monsignor) on Nov 18, 2013 at 11:02 UTC | |
by lolz (Initiate) on Nov 18, 2013 at 11:28 UTC | |
by hdb (Monsignor) on Nov 18, 2013 at 11:44 UTC |