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

Hash table issue

by Anonymous Monk
on May 08, 2004 at 21:04 UTC ( [id://351763]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks , I have this littel issue and need your help. I have the following loop that is reading input and inserting it inside a hash table:
my $x=1; while($x <= $EMPnum) { my $t1 = $left1->Label(-text=>"Guy #$x", -background=>'gray')->pack(); my $num = $left2->Entry(-width=>12, -background =>'white', -borderwidth=>2,-relief=>'sunken', -textvariable => \$info{EmpNum}[$x])->pack(); $x++; }
I am having problem printing the output of the hash $info{EmpNum} , I am trying the following :
foreach (@{$info{EmpNum}}) { print "\t$info{$_}>[0] $info{$_}->[1]\t$info{$_}->[2]\t$info +{$_}->[3]\n"; }
but it gives me nothing . thanks

Replies are listed 'Best First'.
Re: Hash table issue
by Zaxo (Archbishop) on May 08, 2004 at 21:28 UTC

    You have a couple of errors in your print loop, corrected below

    foreach (keys %info) { print "\t$info{$_}->[0]\t$info{$_}->[1]\t$info{$_}->[2]\t$info{$_} +->[3]\n"; }
    You can clean it up further like this,
    for (values %info) { print "\t", join("\t", @$_), "\n"; }

    After Compline,
    Zaxo

      thanks Zaxo , but the issue I am still having is I am using the same hash table for somthing elese . Is it better to create a new hash table or can I do the following :
      foreach (keys %info{EmpNum}) { print "\t$info{$_}->[0]\t$info{$_}->[1]\t$info{$_}->[2]\t$info{$_} +->[3]\n"; }
      I have $info{time} , $info{list} ...just wondering if I can use the same one. thanks
Re: Hash table issue
by krusty (Hermit) on May 08, 2004 at 21:36 UTC
    From the looks of your code, you have a hash %info, which contains only one key "EmpNum" and the value associated with that key is a reference to an anonymous array. At no point in time do I see you populating the elements of your anonymous array, but instead I see you passing a reference to $info{EmpNum}[$x] where $x is some index value you increment.

    Is there even such an element $info{EmpNum}[1]?

    Nor do I see you creating any other keys to this hash %info except for "EmpNum". So I don't see what array you are iterating through when you do your foreach loop or what you are printing out.
Re: Hash table issue
by Errto (Vicar) on May 09, 2004 at 00:32 UTC

    I don't know Tk all that well but I assume that -textvariable expects to be given a reference to where it will eventually store the value from that widget. As written, it will store these in successvie elements of @{$info{EmpNum}}. I'm going to assume you have some valid reason for creating this array as a reference value in a hash with no other keys, rather than an ordinary array.

    However, your foreach loop doesn't look right. What you are iterating over are the elements of the array,  @{$info{EmpNum}} yet you are treating each of these as if it is a key in %info that in turn returns an array of four elements. If true, then I have to assume this happens somewhere else, because your first block of code doesn't create anything like that. In any case you have a typo. $info{$_}>[0] should be $info{$_}->[0] or simply $info{$_}[0].

    Update: I didn't fully read the thread above before posting. You can certainly do:

    foreach (keys %info) { print "\t$info{$_}->[0]\t$info{$_}->[1]\t$info{$_}->[2]\t$info{$_} +->[3]\n"; }

    Note the change from what you posted (%info instead of %info{EmpNum}). But again, given the code you posted, this will print only one row, because the only key in %info is 'EmpNum'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-18 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found