Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Values changing

by Caito (Initiate)
on May 12, 2010 at 21:46 UTC ( [id://839744]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks. I'm a begginer.

I´m using Perl/Tk to make an small aplication... I have one line with two Entry fields and I need store each pair of entry in a hash pair key-value. Until this it's nice but user should can add new lines and for this I need store more hash pairs. How can I store new entries in hash?

My code:

#usr/bin/perl -w use Tk; $mw = MainWindow->new(); $mw->title ("mkFast-A"); $msgi = $mw->Label (-text => 'Making Easy things Easy', -relief => 'groove'); $msgi->pack(-side=>'top'); $button = $mw -> Button(-text=> '+', -command =>\&makeline); $button->pack(-side=>'bottom', -anchor=>'w'); $but = $mw ->Button(-text=>'hash', -command =>\&print_hash); $but->pack(-side => 'bottom', -anchor =>'e'); $but2 =$mw ->Button(-text=>'print', -command =>\&print_seq); $but2->pack(-side => 'bottom', -anchor =>'e'); $x = $$a; $y = $$b; $hash{$x} = $y; print (($x, $y) = each %hash); MainLoop(); sub makeline{ my $i; my $j; my $Seqname = $mw-> Scrolled ('Entry', -scrollbars =>'os'); $Seqname->pack(-side=> 'left'); $i = $Seqname->get(); my $Sequence = $mw -> Scrolled ('Entry', -scrollbars =>'os'); $Sequence->pack(-side=>'right'); $j = $Sequence->get(); $a=\$i; $b=\$j; return $a; return $b; } sub print_hash{ while (($key, $value) = each %hash){ print "$key => $value\n"; } } sub print_seq{ print "$i"; }
Tx

Edit: g0n: code tags

Replies are listed 'Best First'.
Re: Values changing
by liverpole (Monsignor) on May 12, 2010 at 22:39 UTC
    Hi Caito,

    First, please format your question putting tags <code> and </code> around your code.  Otherwise it's very difficult to read.

    Secondly, try putting:

    use strict; use warnings;
    at the beginning of your program.  Once you've fixed the errors that it gives you, you may notice that when you try to print the value of $i in the subroutine print_seq that its value is undefined:
    Global symbol "$i" requires explicit package name at tkprog.pl line 55 +.

    That's because in the subroutine makeline() you are making $i lexically-scoped:

    sub makeline { my $i; my $j; ... }
    so you won't be able to see the value of that variable $i.  To make $i (or other variables) global, just defined them at the top of your program.

    Another thing -- you shouldn't be using the variables $a and $b; they have special meaning in a Perl script (they are used for sorting -- see http://perldoc.perl.org/functions/sort.html for information).

    You're not using the values returned from makeline, nor are you using the values $i and $j anywhere (except that you're assigning them in one place each).

    One other problem you have is that when you create the Scrolled 'Entry' widget, there's nothing in them yet.  So that's not the time to be fetching the contents.  You may want to keep those Entry widgets in a global array, and create another subroutine (executable via a new button) to grab the values of all the Entry widgets when you're ready to use them.

    That's just a start ... please try fixing the formatting of your post, and give more explicit details about what you're trying to do, and what's going wrong, and we'l try to help you out.

    For reference, here's what I think you should have for code so far:

    #!usr/bin/perl -w # Libraries use strict; use warnings; use Tk; # Globals go here my %hash; my $i; my $j; # Main program my $mw = MainWindow->new(-title => "mkFast-A"); my $msgi = $mw->Label(-text => 'Making Easy things Easy', -relief => ' +groove'); $msgi->pack(-side =>'top'); my $button = $mw -> Button(-text=> '+', -command =>\&makeline); $button->pack(-side=>'bottom', -anchor=>'w'); my $but = $mw ->Button(-text=>'hash', -command =>\&print_hash); $but->pack(-side => 'bottom', -anchor =>'e'); my $but2 =$mw ->Button(-text=>'print', -command =>\&print_seq); $but2->pack(-side => 'bottom', -anchor =>'e'); # This section isn't doing anything useful # my $x = $$a; # my $y = $$b; # $hash{$x} = $y; # print (($x, $y) = each %hash); MainLoop(); sub makeline { my $Seqname = $mw-> Scrolled ('Entry', -scrollbars =>'os'); $Seqname->pack(-side=> 'left'); $i = $Seqname->get(); print "i is $i\n"; my $Sequence = $mw -> Scrolled ('Entry', -scrollbars =>'os'); $Sequence->pack(-side=>'right'); $j = $Sequence->get(); } sub print_hash { my ($key, $value); while (($key, $value) = each %hash) { print "$key => $value\n"; } } sub print_seq { print "$i"; }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Log In?
Username:
Password:

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

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

    No recent polls found