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

How to read every line of file into array

P: 8
This program display a names. If the name you type match the one asked to type, the program tells you the phone number of the name. The problem is it only list the last name in the file. How to get the program to start at the first name in the file, and after you type it correct, show name number and move to the next name? All help would be graceful!!
Expand|Select|Wrap|Line Numbers
  1. open (FILE, "profile.txt") || die "Can't open File.txt: $!\n";
  2. @raw_data=<FILE>;
  3. close(FILE);
  4.  
  5. # my array only loads last name in file
  6. foreach $_ (@raw_data)  #foreach loop to grab wanted variables
  7. {
  8.  ($c_name, $descript_info)=split(/\|/, $_); #split method create pipe used escaped with \ char  two variables one for the names other for numbers
  9.  
  10. sub go{
  11. $user= "Please type the name  $c_name \n";}
  12. MainLoop;
  13.  
  14. sub enter{ 
  15. if ($E) #check user input value
  16. { goto contin1;} 
  17.  
  18. else
  19. {$user= "You didn't type anything!!\n"; 
  20. }
  21.  
  22. contin1:
  23. $user= "\e[H\e[J"; #clear the screen
  24. if ( $c_name =~ /$E/ ) #compare with user input
  25.  {
  26.     $user="The name -$c_name to $desricpt_info \n"; 
  27. my $mw_next=$main->messageBox(-message => "Remember!",-=>\&go, -type =>$user);
  28.  } 
  29. else 
  30.  {  
  31.     $user= "The name was mistyped! Please try again.\n";
  32.  }
  33.  
Mar 25 '10 #1

✓ answered by RonB

First, ALWAYS use the code tags when posting your code.

Add these 2 pragmas and fix the problems they point out.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Get rid of that goto statement. The goto statement is very rarely needed or appropriate, and is never needed/appropriate in this type of situation.

Load your data into a hash, not an array.

Share this Question
Share on Google+
14 Replies


Expert Mod 100+
P: 589
First, ALWAYS use the code tags when posting your code.

Add these 2 pragmas and fix the problems they point out.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Get rid of that goto statement. The goto statement is very rarely needed or appropriate, and is never needed/appropriate in this type of situation.

Load your data into a hash, not an array.
Mar 25 '10 #2

P: 1
I believe your file is getting into your array ok, but when you are looping through it, you are only saving the last row.

The loop splits the row fine, but does nothing with the values until it terminates. At that point the values are set to whatever was found in the last row.

Incidentally - you don't have to read the file into an array - you can loop across your <file> this way:
Expand|Select|Wrap|Line Numbers
  1. open FILE, "profile.txt" || die "Can't open File.txt: $!\n";
  2. foreach $_ (<FILE>) #foreach loop to grab wanted variables {
  3.      #do ALL your stuff here
  4. }
  5. close FILE;
  6.  
The most important thing is, do everything you need to do inside the loop - don't read all your data in and exit the loop with only the last element in the array (the last line in your file).

I hope this helps!
Mar 25 '10 #3

Expert Mod 100+
P: 589
Incidentally - you don't have to read the file into an array - you can loop across your <file> this way:

open FILE, "profile.txt" || die "Can't open File.txt: $!\n";
foreach $_ (<FILE>) #foreach loop to grab wanted variables {
#do ALL your stuff here
}
close FILE;

The most important thing is, do everything you need to do inside the loop - don't read all your data in and exit the loop with only the last element in the array (the last line in your file).
Most of the time I'd agree with that, but in this case that would be the wrong approach.

If the data is going to be queried multiple times, as it is in this case, it would be better to process the file once and store it in a data structure. The choice then becomes, what type of data structure. Since each line is a "record" of name/value pairs, storing the data in a hash becomes the most obvious and efficient data structure. Then it's a simple matter of doing a hash lookup to see if the user supplied name is in the hash.
Mar 25 '10 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
And @soundconcept, please use CODE TAGS around your code, just as was suggested to the OP who started this thread, but RonB.

Code tags are required around ALL CODE that is placed into the forums. If you do not use them, then we have to clean up behind you and put them in place for you.

If you do not know how to use them, then maybe you should read How To Post A Question in the sites FAQ.
Mar 25 '10 #5

P: 8
I should had tried to put my data set in a hash from the beginning. Hashes are good for handling large data sets.

Thank you to everyone that responded and I will get back to you to tell you how it goes.
Mar 28 '10 #6

P: 8
Sorry about the missing code tags.
Mar 28 '10 #7

P: 8
I forgot to mention the array works fine in perl with is text-based. But the problem is using this coding for perl tk. Im having difficulties with this event-driven part. All help will be considered an honor.
Mar 30 '10 #8

Expert Mod 100+
P: 589
We can't help you troubleshoot code that you haven't shown and haven't properly explained the problem.
Mar 30 '10 #9

P: 8
The problem is using this coding for perl tk. Im having difficulties with this event-driven part. The problem is this program is only displaying the last line in the file. Need it to display each name, and after the buttom is pressed display the next name. All help will be considered an honor.
Expand|Select|Wrap|Line Numbers
  1.  
  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.  
  10. $mw->Button (-text=>"names",
  11.           -command=>[\&printstrings,$_])
  12. ->pack(-side=>"left");
  13.  
  14. $mw->Label(-textvariable=>\$user)->pack();
  15. sub printstrings 
  16. {open (FILE, "new.txt") || die "Can't open File.txt: $!\n";
  17.  
  18. foreach $_ (<FILE>)
  19. {
  20. ($c_name, $phone_info)=split(/\|/, $_);
  21.  $user= "the word $c_name $phone_info\n";
  22. }}
  23.  
  24. MainLoop(); 
  25.  
  26.  
Mar 31 '10 #10

Expert Mod 100+
P: 589
The problem is that you're over writing $user for each line in the file, which is why you end up with only the last line.

There are several other issues, but to fix this one problem, change this line:
Expand|Select|Wrap|Line Numbers
  1. $user= "the word $c_name $phone_info\n";
To this:
Expand|Select|Wrap|Line Numbers
  1. $user .= "the word $c_name $phone_info\n";
Mar 31 '10 #11

P: 8
Thanks RonB for the insight but how to get it to display each name after I press the button?
Apr 1 '10 #12

Expert Mod 100+
P: 589
I'm not sure what you mean.

After making the correction I showed, it will display each/all names. Assuming that the lines are formatted as you expect and the split is successful.
Apr 1 '10 #13

P: 8
What I mean is how to display one name at a time when the button is pressed instead of showing the whole list all at one time.
Apr 1 '10 #14

Expert Mod 100+
P: 589
First, load the data file into an array. This would be done outside of the printstrings sub.

The sub would track the last array index number that it accessed and update the screen with the next array element.

I'd use a text or listbox widgit instead of the label widgit.

Here's an example of each.
http://facultyfp.salisbury.edu/taana...xtExample.html
http://www.bin-co.com/perl/perl_tk_t...k_commands.php
Apr 1 '10 #15

Post your reply

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