fame has asked for the wisdom of the Perl Monks concerning the following question:

if($msg =~ /!info\s+(.{6})/) { my $info = $1; my $database = "list.txt"; open FILE,"$database" or die "Error Opening: $!"; my @results = <FILE>; close FILE; my $var = 0; my $chkvar = 0; foreach my $url(@results) { if($url =~ m/$info\|(.*)\|(.*)/) { if($var == 0) { $var = 1; sendmsg($irc_socket, "PRIVMSG $channel &#9 +219;4,1[&#9219;0,1BANK&#9219;4,1]&#9219;9,1 $1"); goto finish; } }else{ $chkvar = 1; } } if($chkvar == 1) { sendmsg($irc_socket, "PRIVMSG $channel &#9219;4,1[ +&#9219;0,1BANK&#9219;4,1]&#9219;7,1 Could not retreive bank"); } finish: }

i have this piece of code from an irc bot and what it does it read the first 6 digits from the file when someone types !bank 123456 and display whats after it.. example of text file:

518767|CAIXA ECONOMICA FEDERAL|LA BRA BRASILIA |BRAZIL|

Now I had a question, since im no guru at all in perl. How oculd I change this function to say like the command be !state and the person says: !state NY

it reads from the file and it returns the results as State NY = > New York.

ive tried the same concept as above and made the abbreviations of the states and text file looking like: NY|New York but did not have any sucesss.. could someone point me in the right direction?

Update: Thanks

Replies are listed 'Best First'.
Re: Opening and reading file
by poj (Abbot) on Jan 15, 2012 at 21:21 UTC
    if ($msg =~ /!state\s+(.{2})/){ my $search = uc($1); my $result; my @list = <DATA>; for (@list){ chomp; my ($id,$name) = split /\|/,$_; if ($id eq $search){ $result = "State $id = > $name\n"; last; } } # if ($result){ print $result; } else { print "Could not find state $search\n"; } } __DATA__ CA|California ME|Maine NY|New York TX|Texas UT|Utah
    poj
A reply falls below the community's threshold of quality. You may see it by logging in.