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


In reply to Re^2: Verify Town Name by PilotinControl
in thread Verify Town Name by PilotinControl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.