By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,835 Members | 2,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,835 IT Pros & Developers. It's quick & easy.

Foreach looping an array in perl tk.

P: 8
This program is suppose to only display one name and number after the buttom is pressed. But the problem is it only displays the first name and number after the button is pressed. It won't display any other names or numbers but the first line. The program is not reading each line one at a time per button press. Only continue to display the first line in the file after the button is pressed. All help will be graceful!

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. require Tk;
  3. use Tk ':eventtypes';
  4. use Tk;
  6. my $mw = MainWindow->new();
  8. open (FILE, "new.txt") || die "Can't open File.txt: $!\n";
  9. $raw_data = <FILE>;    
  11. foreach $_ ($raw_data)
  12. {
  13. chomp ($_);
  14. ($c_name, $desricpt_info)= split(/\|/, $_);
  15. $mw->Button (-text=>"two strings",
  16.           -command=>[\&printstrings,$_])
  17. ->pack(-side=>"left");}
  18. $mw->Label(-textvariable=>\$user)->pack();
  20. sub printstrings 
  21. {
  22. $user .= "the name $c_name $desricpt_info\n";
  23. }
  25. MainLoop();
Apr 7 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 100+
P: 589
Why are you attempting to load the Tk module 3 times? You only need to do it once.

Remove the -w switch and add these 2 pragmas.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Fix all problems that they point out.

Fix your indentation.

$raw_data is a single scalar item, not a list, so why are you using a foreach loop?

For displaying a new line each time the button is pressed, did you try the suggestion I gave you in your other question?
Apr 7 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.