473,413 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Foreach looping an array in perl tk.

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;
  5.  
  6. my $mw = MainWindow->new();
  7.  
  8. open (FILE, "new.txt") || die "Can't open File.txt: $!\n";
  9. $raw_data = <FILE>;    
  10.  
  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();
  19.  
  20. sub printstrings 
  21. {
  22. $user .= "the name $c_name $desricpt_info\n";
  23. }
  24.  
  25. MainLoop();
  26.  
Apr 7 '10 #1
1 2444
RonB
589 Expert Mod 512MB
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

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

Similar topics

1
by: Adam | last post by:
Hi, I am having a rather frustrating time with a script I am working on. I am looping through the elements of an array with a foreach statment and inside the foreach statement I have two if...
7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
4
by: Jack E Leonard | last post by:
I'm looping through the keys and values of a form submission using foreach($_POST as $key => $value) echo "$key = $value<br>"; No problems there. All works as expected. But seveal of the...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
4
by: e_matthes | last post by:
Hello everyone, I have no trouble looping through an array using foreach. To keep track of the index, I set a variable before the loop and increment it in the loop: $index = 0; foreach...
13
by: Tamagafk | last post by:
Hi! Looks like there is a bug in php. If I have function which uses foreach to run trough array recursively, the lop-level foreach interupted by lover-level foreach'es. If I use simply 'for'...
28
by: natarajmtech | last post by:
Hi all, I have defined some array variables a ***************************************** @L=('CTT','CTC','CTA','CTG','TTA','TTG'); @S=('TCT','TCC','TCA','TCG','AGT','AGC'); ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.