Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Problem with hash

by padawan_linuxero (Scribe)
on Jun 03, 2008 at 16:32 UTC ( [id://689932]=perlquestion: print w/replies, xml ) Need Help??

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

I have been working on my pet project and with the help of some monks my project is continue to grow, at last I have a program that actually does what I want, but always a but, is marking me an error everytime it runs, the error is these one :Use of uninitialized value in hash element at test5.pl line 49. I have been reading about the hash and to me the program makes sense to what Ellie Quigley said on the book
can someone see the problem?

this is the code:
#!/usr/bin/perl -w use warnings; use strict; use Tk; my @banklist = qw(Banamex HSBC Santander Banorte); my $mw = MainWindow->new(); my %data; foreach my $bank (@banklist) { $data{$bank}{'frame'} = $mw->Frame->pack(-fill=>'x'); $data{$bank}{'bankname'} = $bank; $data{$bank}{'status'} = 'offline'; $data{$bank}{'label'} = $data{$bank}{'frame'}->Label( -textvariable => \$data{$bank}{'bankname'})->pack(-side=>'left'); $data{$bank}{'entry'} = $data{$bank}{'frame'}->Entry( -textvariable => \$data{$bank}{'status'}, -bg => 'white', )->pack(-side=>'right'); } $mw->repeat(10000, \&CheckForActiveBanks); sub CheckForActiveBanks { my %statusList = (); #-- Reset the list of bank statuses. # foreach my $bank (@banklist) { $statusList{$bank} = 'X'; } #-- Read in the data file. # open (DAT, "santa1.txt") || die ("No se pudo abrir el archivo"); my @lines = <DAT>; close(DAT); #-- Set the status for each bank found in the data file. # foreach my $lineas (@lines) { chomp $lineas; my $statuss; my $banco; ($banco, $statuss) = split(/\|/, $lineas); $statusList{$banco} = $statuss; } #-- Update the data for the list of banks. # foreach my $bank (@banklist) { if ($statusList{$bank} eq "ACTIVO") { $data{$bank}{'entry'}->configure(-fg => 'green'); $data{$bank}{'status'} = 'online'; } else { $data{$bank}{'entry'}->configure(-fg => 'red'); $data{$bank}{'status'} = 'offline'; } } } MainLoop;

Replies are listed 'Best First'.
Re: Problem with hash
by kyle (Abbot) on Jun 03, 2008 at 16:37 UTC

    It would help if you'd point out the line generating the warning. I think it's this one:

    $statusList{$banco} = $statuss;

    My guess is $banco is undef, probably because there's a blank line in santa1.txt.

      Yes what a moron I am
      that was the problem thank you it had a another line in the end of the text file

        You're probably already doing it this way but, just in case you're not: instead of just deleting the line, you might want to consider making the script more robust -- check that $banco and $statuss are defined before using them.

        Just a thought.

        --
        Wade
Re: Problem with hash
by wade (Pilgrim) on Jun 03, 2008 at 16:41 UTC

    I'll bet that $lineas has no '|' on that line so that $statuss is undefined, there. Still, I wouldn't expect an error message.

    It works fine for me under ActiveState (5.8.8) on XP. I don't have your data files, so I'm guessing that it's data-related

    --
    Wade

      Yeah, kyle was right. I had assumed that 'split' would put something in $banco if the line was defined but, after testing, I discovered that it didn't. $banco is left undefined.

      --
      Wade
Re: Problem with hash
by apl (Monsignor) on Jun 03, 2008 at 16:55 UTC
    Did you try displaying $lineas and $banco before the offending statement?

    Did you try modifying your code so that $banco had to be defined before using it as the key to a hash?

Re: Problem with hash
by starbolin (Hermit) on Jun 03, 2008 at 16:58 UTC

    kyle gives excellent advice. I would also check that $statuss is defined before assigning it to your hash otherwise you'll get an 'undefined' warning when you later access that key.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-18 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found