Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Check For Dupes In FLat DB Before Adding

by northwind (Hermit)
on Dec 14, 2005 at 13:39 UTC ( [id://516726]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Check For Dupes In FLat DB Before Adding

Reading between the lines of the OP, lets say the flat file is variable length records with one value per line and an empty line seperates records.  Then this code should work:

open(FILE_HANDLE, ">", "$filename") || die "ACK, GASP: $error_message: + $!\n"; my @data = <FILE_HANDLE>; chomp(@data); close FILE_HANDLE; my %db_hash; my $unique_id = 0; foreach (@data) { next if(m/^\s*$/); my %temp_hash = (); s/^\s+//; # Remove excess whitespace s/\s+$//; m/^(.*?)=(.*?)$/i; # Grab key/value pairs $temp_hash{$1} = $2; $db_hash{$unique_id} = \%temp_hash; $unique_id++; } open(NEW_FILE, "<", "$new_filename") || die ACK, GASP: $error_message: + $!\n"; foreach my $id (sort keys %db_hash) { if(not defined $db_hash{$id}{email}) { $db_hash{$id}{email} = $some_value; } foreach (sort keys $db_hash{$id}) { print NEW_FILE "$_=$db_hash{$id}{$_}\n"; } print "\n"; } close NEW_FILE;
(Note:  Code is untested!  Use at your own risk.  Also, there are a huge number of assumtions made in the above code because the OP was more than a little vauge...)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://516726]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.