Re: Write in files
by davido (Cardinal) on Nov 18, 2013 at 07:50 UTC
|
Have a look at Text::CSV, and then once you've read through its documentation feel free to follow up with questions that remain unanswered.
| [reply] |
Re: Write in files
by CountZero (Bishop) on Nov 18, 2013 at 12:59 UTC
|
Personally, I would use DBI and DBD::CSV to do this.If you would look at your two files as tables in a database, then it becomes a matter of a simple SQL query to extract the info and export it to a new table with all info combined.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] |
Re: Write in files
by taint (Chaplain) on Nov 18, 2013 at 07:57 UTC
|
Greetings, lolz
I suspect that you will do well to learn about
Perl regular expressions (RE) (perlfaq6), as well, if you don't already.
--Chris
#!/usr/bin/perl -Tw
use Perl::Always or die;
my $perl_version = (5.12.5);
print $perl_version;
| [reply] |
Re: Write in files
by hdb (Monsignor) on Nov 18, 2013 at 08:11 UTC
|
You would read the names and phone numbers from file 1 into a hash with the names as keys and then do a lookup on the hash while reading file 2.
| [reply] |
Re: Write in files
by lolz (Initiate) on Nov 18, 2013 at 08:25 UTC
|
Thank you guys for your help. I will have a look on Text:CSV as soon as possible.
The RegEx aren't the Problem. I know how to match the Strings. The Problem is that i don't know how to overwrite the line with my new line, I'll give you an example:
my first thoughts were something like this:
open FILEHANDLE, "/root/Desktop/file1.txt";
open FILEHANDLE2,">>", "/root/Desktop/file2.txt";
while (my $line = <FILEHANDLE>) {
$line =~ /RegEx/i;
my $telefonnummer = $1;
my $name = $2;
foreach (my $line2 = <FILEHANDLE2>) {
$line2 =~ /RegExp/i;
my $name2 = "$1,$2";
if ($name =~ /$name2/i) {
$line2 = "$line2;+49$telefonnummer"; }
}
}
I hope that it doesn't look to strange :)
The Problem in this code is that i can't simply print the
$line2 = "$line2;+49$telefonnummer";
line in the file.
If that Code wont work at all please tell me, if it would work with some modifications please tell me too. :D
Thanks.
| [reply] [d/l] [select] |
|
|
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. | [reply] [d/l] [select] |
|
|
I have a few questions to your code.
what exactly does the code
$verzeichnis{lc $name} = $telefonnummer;
and
if( defined $verzeichnis{lc $name2} ) {
print $new ";+49".$verzeichnis{lc $name2};
}
does?
I haven't worked with defined and lc yet...
Thanks for your efforts! | [reply] [d/l] [select] |
|
|
|
|
|
|
|
One problem you will have, if you try to update files 2, is that you can not write a new, longer line, into the middle of file 2, without overwriting the following data:
# Original file 2:
Bloggs,Fred\n
Conner,Sarah\n
Bloggs,Joe\n
# file 2 after adding Fred's phone Number
Bloggs,Fred,0234687821\n
h\n
Bloggs,Joe\n
As you can see, poor old Sarah Conner, has been terminated. Follow the advice to build a new file, and if required remove (or rename) the old file 2, and rename the new one, to the original name, when done.
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
| [reply] [d/l] |
|
|
how would the code be for editing the original file?
just because i want to know how that would work :)
| [reply] |
|
|
Re: Write in files
by lolz (Initiate) on Nov 20, 2013 at 13:39 UTC
|
heyho,
I'm about to solve that Problem. At least i think i'll do.
My Problem now is, that i have to search threw the values of %Verzeichnis.
And if the Skalar und the value are eq,the key of the value should be written into a file.
Maybe there is some function to search threw the Hash Values? And to find out the key for the value?
Sorry, but i have not worked with Hashes so much, yet.
Thank you!
| [reply] |
|
|
You can use reverse to reverse a hash: keys become values, values become keys. One has to be careful though, if there are duplicate values (two persons sharing a phone number) only one entry will survive. In addition, if the values are not strings, Perl will stringify them, which can have funny effects.
You can also use grep like this:
my @keys = grep { $hash{$_} eq $some_value } keys %hash;
which can return multiple values keys.
Update: some example
my %verzeichnis = (
peter => 12345,
paul => 34567,
mary => 12345
);
my @names = grep { $verzeichnis{$_} eq '12345' } keys %verzeichnis;
print "@names\n";
my %rueckwaertssuche = reverse %verzeichnis;
print $rueckwaertssuche{'12345'}, "\n"; # lost mary
| [reply] [d/l] [select] |