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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.