Thank You @Discipulos your code worked. Now I need to compare the Industry from another file whether it exists or not. Same type of foreach loop. If it exists it moves on if not it circles back to itself. Would this be considered a nested foreach loop? My current working code to find Town Name is below. Would I add another foreach loop below the current one? I need to save the variables from each user input and send it to a file which I already have coded. Thanks for the help!
#!/usr/bin/perl -w
use strict;
use warnings;
begin();
sub begin {
open(MYINPUTFILE, "towndata.txt") or die $!; # OPEN FOR INPUT
$| = 1;
my @lines = <MYINPUTFILE>; # READ FILE INTO LIST
@lines = map {chomp $_; $_} @lines;
close (MYINPUTFILE);
print "LOCATION TOWN:\n";
my $loctwn = <STDIN>;
$loctwn = <STDIN> until defined $loctwn;
chomp $loctwn;
my $found = 0;
foreach my $loctownverify (@lines) {
my @field = split(':',$loctownverify);
if ( $field[1] eq $loctwn ){
print "FOUND $loctwn\n";
$found++;
sleep 1;
print "INDUSTRY NAME:\n";
my $locindstanm = <STDIN>;
$locindstanm = <STDIN> until defined $locindstanm;
chomp $locindstanm;
print "LOCATION SUCCESSFULLY ADDED!!\n";
sleep 3;
exit;
} # END IF
} # END FOR EACH LOOP
print "[$loctwn] RECORD NOT FOUND\n", unless $found;
sleep 3;
begin();
} # END BEGIN SUB
UPDATE
I have figured out how to check two items....now I'm stumped on how to go back to the second verification if an item does not exist.
#!/usr/bin/perl -w
use strict;
use warnings;
begin();
sub begin {
no warnings 'redefine';
no warnings 'uninitialized';
my $found = 0;
my $foundtwo = 0;
open(MYINPUTFILE, "towndata.txt") or die $!; # OPEN FOR INPUT
open(MYINPUTFILETWO, "industrydata.txt") or die $!; # OPEN FOR INPUT
$| = 1;
my @lines = <MYINPUTFILE>; # READ FILE INTO LIST
my @linestwo = <MYINPUTFILETWO>; # READ FILE INTO LIST
@lines = map {chomp $_; $_} @lines;
@linestwo = map {chomp $_; $_} @linestwo;
close (MYINPUTFILE);
close (MYINPUTFILETWO);
print "LOCATION TOWN:\n";
my $loctwn = <STDIN>;
$loctwn = <STDIN> until defined $loctwn;
chomp $loctwn;
foreach my $loctownverify (@lines) {
my @field = split(':',$loctownverify);
if ( $field[1] eq $loctwn ){
print "FOUND $loctwn\n";
$found++;
sleep 1;
print "INDUSTRY NAME:\n";
my $locindstanm = <STDIN>;
$locindstanm = <STDIN> until defined $locindstanm;
chomp $locindstanm;
foreach my $locindstaverify (@linestwo) {
my @fieldtwo = split(':',$locindstaverify);
if ( $fieldtwo[2] eq $locindstanm ){
print "FOUND $locindstanm\n";
$foundtwo++;
sleep 1;
print "LOCATION SUCCESSFULLY ADDED!!\n";
my $output="locindstadata.txt";
open(DAT,"+<$output") || die("Cannot Open File"); # NEW
my $locindstaline;
$locindstaline = <DAT> until eof DAT;
my ($locindstaid) = $locindstaline =~ m/\A(\d+):/;
print DAT (++$locindstaid);
print DAT (":");
print DAT ($loctwn);
print DAT (":");
print DAT ($locindstanm);
print DAT ("\n");
close (DAT);
sleep 3;
exit;
} # END IF
} # END FOR EACH LOOP
print "[$locindstanm] RECORD NOT FOUND\n", unless $foundtwo;
}
}
print "[$loctwn] RECORD NOT FOUND\n", unless $found;
sleep 3;
begin();
} # END BEGIN SUB
|