Hi

I have  written a small script to input  customer data in to a flat file,  i
would like some guidance on how i could write this script better , i would
also like to know if i have optimally used HoH properly .

-----------------------------------------------------------------------------------------
[r...@localhost scripts]# cat  data_insert.pl
#!/usr/bin/perl -w
# this is a test script trying to play around with perl
# A. ask for name , age, address, emailID  phone number  and put data in
flat file  if email iD exist then exit ;
use warnings;
use strict;
use Data::Dumper ;

my $txt = '/scripts/data.txt';

my %read_data = &get_data;

print Dumper ([\%read_data]);

# on print of %read_data  here is the output
##$VAR1 = [
#          {
#            'to...@123.com' => {
#                                 'name ' => ' tommy A',
#                                 'emailid ' => ' to...@123.com',
#                                 'address ' => ' street 123 NY US ',
#                                 'age ' => ' 33'
#                               },
#            'agnello.dso...@mail.com' => {
#                                           'name ' => ' agnello',
#                                           'emailid ' => '
agnello.dso...@mail.com',
#                                           'address ' => ' c 123 andheri
mumbai',
#                                           'age ' => ' 25'
#                                         }
#          }
#        ];
#


my %database;
$/="\n";

my @allask = qw(name age address emailid );
foreach my $ask (@allask) {
  print " please enter $ask:";
  while ( chomp($database{$ask} = <STDIN>) ) {
    if ( $read_data{$database{$ask}}{'emailid '}  ) { # if emailid exist
      print "$database{$ask} already exist \n";
      exit;
    }
    last if $database{$ask};
    print "please enter $ask:  ";
  }
}

open(OUTFILE,">>","$txt") or die " cannot open file : $!";
foreach (@allask) {
  print OUTFILE "$_ : $database{$_}\n";
}
print OUTFILE "\n";
close (OUTFILE);

#--------------------

sub get_data () {

  $/="";

  my %hoh = ();
    open(IN1,"<","$txt" ) or die " cannot opne file $!" ;
  my $ass;
  while ( $ass = <IN1>) {
    chomp ($ass);
    my ($email) = reverse split (/:/, "$ass" ) ;
    $email =~ s/\s+//g;
    foreach (split (/\n/,$ass) ) {
      $hoh{$email}{$1} = $2 if /(.*):(.*)$/ ;
    }
  }

  return %hoh;
  close(IN1);
}

-- 
Regards
Agnello D'souza

Reply via email to