EngAmira
Newbie level 6
I've a aproblem with perl script not executed as required
its function is employee bank system which used to add names and salary and title, search certain entry , delete entry ,edit entry
the following code show the subrotine for add entry, search entry
the add part works properly, but the search part has a problem that it can't detect the searched name !!, count should incremented as it found the search name on the array.... any one can help
#!/usr/bin/perl
# your code goes here
my @firstname= ();
my @secondname= ();
my @thirdname= ();
my @title= ();
my @salary= ();
my $i=0;
my $ID=0;
while (1) {
my $choice = menu();
if ($choice == 1) {
addrecord();
} elsif ($choice == 2) {
editrecord();
} elsif ($choice == 3) {
deleterecord ();
} elsif ($choice == 4) {
sortnumber();
} elsif ($choice == 5) {
searchentry();
} elsif ($choice == 6) {
exit();
} else {
print "Invaild Entry Try again";
}
}
###############################################
sub menu {
print"1. Add record.\n";
print"2. Edit an existing record.\n";
print"3. Delete an existing record.\n";
print"4. Sort the database .\n";
print"5. Search by Name, Title, Salary (less than , more than).\n";
print"6. Quit.\n";
print"Enter your choice (1-6): ";
my $prompt = <STDIN>;
return $prompt;
}
###########################################################
sub addrecord {
Again_name:
print"Enter first name: \n";
my $firstname= <STDIN>;
$firstname = lc($firstname);
chomp $firstname;
print"Enter second name: \n";
my $secondname= <STDIN>;
$secondname = lc( $secondname);
chomp $secondname;
print"Enter third name: \n";
my $thirdname= <STDIN>;
$thirdname= lc($thirdname);
chomp $thirdname;
if (grep /$firstname/,@firstname){
if (grep /$secondname/,@secondname){
if (grep /$thirdname/,@thirdname){
print"Entry already exists\n";
goto Again_name;
}
}
}
push @firstname,$firstname;
push @secondname,$secondname;
push @thirdname,$thirdname;
print"Enter Titels.: \n";
$title= <STDIN>;
chomp $title;
push @title,$title;
print"Enter Salary.: \n";
$salary= <STDIN>;
chomp $salary;
push @salary,$salary;
$i++;
$ID=$ID+1;
for($index=0;$index<=$#firstname;$index++)
{
print "$ index $firstname[$index] $secondname[$index] $thirdname[$index]: $title[$index]: $salary[$index] \n";
}
}
#################################################################
#search
sub searchentry {
my $count=0;
my $count1=0;
print"first name: \n";
my $searchname1 = <STDIN>;
$searchname1 = lc($searchname1);
foreach (@firstname){
++ $count if $_ ==$searchname1;
print"count=$count \n";
if ($count==1){
my( $index )= grep { $firstname[$_] ==$searchname1} 0..$#firstname;
print "$firstname[$index]\t$secondname[$index]\t $thirdname[$index]: $title[$index]: $salary[$index] \n";
}
elsif($count>1) {
print"Enter second name \n";
my $searchname2 = <STDIN>;
$searchname2 = lc($searchname2);
foreach (@secondname){
++$count1 if $_ ==$searchname2;}
if ($count1==1){
my( $index )= grep { $secondname[$_] == $searchname2 } 0..$#secondname;
print "$firstname[$index]\t$secondname[$index]\t$thirdname[$index]: $title[$index]: $salary[$index] \n";
}
elsif($count1>1) {
print"Enter third name \n";
my $searchname3 = <STDIN>;
$searchname3 = lc($searchname3);
my( $index )= grep { $thirdname[$_] == $searchname3 }0..$#thirdname;
print "$firstname[$index]\t$secondname[$index]\t$thirdname[$index]: $title[$index]:
$salary[$index] \n";
}else{
print"There is no such name in the employee list\n";
}
}else{
print"There is no such name in the employee list \n";
}
}
its function is employee bank system which used to add names and salary and title, search certain entry , delete entry ,edit entry
the following code show the subrotine for add entry, search entry
the add part works properly, but the search part has a problem that it can't detect the searched name !!, count should incremented as it found the search name on the array.... any one can help
#!/usr/bin/perl
# your code goes here
my @firstname= ();
my @secondname= ();
my @thirdname= ();
my @title= ();
my @salary= ();
my $i=0;
my $ID=0;
while (1) {
my $choice = menu();
if ($choice == 1) {
addrecord();
} elsif ($choice == 2) {
editrecord();
} elsif ($choice == 3) {
deleterecord ();
} elsif ($choice == 4) {
sortnumber();
} elsif ($choice == 5) {
searchentry();
} elsif ($choice == 6) {
exit();
} else {
print "Invaild Entry Try again";
}
}
###############################################
sub menu {
print"1. Add record.\n";
print"2. Edit an existing record.\n";
print"3. Delete an existing record.\n";
print"4. Sort the database .\n";
print"5. Search by Name, Title, Salary (less than , more than).\n";
print"6. Quit.\n";
print"Enter your choice (1-6): ";
my $prompt = <STDIN>;
return $prompt;
}
###########################################################
sub addrecord {
Again_name:
print"Enter first name: \n";
my $firstname= <STDIN>;
$firstname = lc($firstname);
chomp $firstname;
print"Enter second name: \n";
my $secondname= <STDIN>;
$secondname = lc( $secondname);
chomp $secondname;
print"Enter third name: \n";
my $thirdname= <STDIN>;
$thirdname= lc($thirdname);
chomp $thirdname;
if (grep /$firstname/,@firstname){
if (grep /$secondname/,@secondname){
if (grep /$thirdname/,@thirdname){
print"Entry already exists\n";
goto Again_name;
}
}
}
push @firstname,$firstname;
push @secondname,$secondname;
push @thirdname,$thirdname;
print"Enter Titels.: \n";
$title= <STDIN>;
chomp $title;
push @title,$title;
print"Enter Salary.: \n";
$salary= <STDIN>;
chomp $salary;
push @salary,$salary;
$i++;
$ID=$ID+1;
for($index=0;$index<=$#firstname;$index++)
{
print "$ index $firstname[$index] $secondname[$index] $thirdname[$index]: $title[$index]: $salary[$index] \n";
}
}
#################################################################
#search
sub searchentry {
my $count=0;
my $count1=0;
print"first name: \n";
my $searchname1 = <STDIN>;
$searchname1 = lc($searchname1);
foreach (@firstname){
++ $count if $_ ==$searchname1;
print"count=$count \n";
if ($count==1){
my( $index )= grep { $firstname[$_] ==$searchname1} 0..$#firstname;
print "$firstname[$index]\t$secondname[$index]\t $thirdname[$index]: $title[$index]: $salary[$index] \n";
}
elsif($count>1) {
print"Enter second name \n";
my $searchname2 = <STDIN>;
$searchname2 = lc($searchname2);
foreach (@secondname){
++$count1 if $_ ==$searchname2;}
if ($count1==1){
my( $index )= grep { $secondname[$_] == $searchname2 } 0..$#secondname;
print "$firstname[$index]\t$secondname[$index]\t$thirdname[$index]: $title[$index]: $salary[$index] \n";
}
elsif($count1>1) {
print"Enter third name \n";
my $searchname3 = <STDIN>;
$searchname3 = lc($searchname3);
my( $index )= grep { $thirdname[$_] == $searchname3 }0..$#thirdname;
print "$firstname[$index]\t$secondname[$index]\t$thirdname[$index]: $title[$index]:
$salary[$index] \n";
}else{
print"There is no such name in the employee list\n";
}
}else{
print"There is no such name in the employee list \n";
}
}