Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Verify Town Name

by Discipulus (Canon)
on Nov 30, 2021 at 07:47 UTC ( [id://11139260]=note: print w/replies, xml ) Need Help??


in reply to Verify Town Name

Hello PilotinControl,

help us to help you giving runnable and correctly indented code :)

Your perLplexing issue can arise from wrong index 1 (instead of 2) in $field[1] ? Use Data::Dump here and there to verify data you are processing.

The following works as expected

use strict; use warnings; my @lines = <DATA>; @lines = map {chomp $_; $_} @lines; my $indtwn = <STDIN>; # $indtwn = <STDIN> until defined $indtwn; ??? chomp $indtwn; my $found = 0; foreach my $townverify (@lines) { my @field = split(':',$townverify); next if $field[0] =~ m/^#/; if ( $field[2] eq $indtwn ){ # <--- here print "FOUND $indtwn\n"; $found++; last; } } print "[$indtwn] NOT FOUND" unless $found; __DATA__ 1:PGH:Pittsburgh 2:PGHW:Pittsburgh West End

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Verify Town Name
by davido (Cardinal) on Dec 01, 2021 at 00:01 UTC

    Time vampires will never "help us to help you giving runnable and correctly indented code :)."


    Dave

Re^2: Verify Town Name
by PilotinControl (Pilgrim) on Dec 02, 2021 at 22:29 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139260]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found