473,804 Members | 3,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read every line of file into array

8 New Member
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
14 16732
RonB
589 Recognized Expert Moderator Contributor
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
soundconcept
1 New Member
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
RonB
589 Recognized Expert Moderator Contributor
Incidentally - you don't have to read the file into an array - you can loop across your <file> this way:

open FILE, "profile.tx t" || 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
3,509 Recognized Expert Moderator Specialist
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
lashaw
8 New Member
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
lashaw
8 New Member
Sorry about the missing code tags.
Mar 28 '10 #7
lashaw
8 New Member
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
RonB
589 Recognized Expert Moderator Contributor
We can't help you troubleshoot code that you haven't shown and haven't properly explained the problem.
Mar 30 '10 #9
lashaw
8 New Member
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

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

Similar topics

5
2988
by: Knackeback | last post by:
task: - read/parse CSV file code snippet: string key,line; typedef tokenizer<char_separator<char> > tokenizer; tokenizer tok(string(""), sep); while ( getline(f, line) ){ ++lineNo; tok.assign(line, sep);
6
23780
by: G.Esmeijer | last post by:
Friends, I would like to read a text file (fixed length formaated) really fast and store the data into an Access database (2003). Using the streamreader and reading line by line, separating the line into string just takes to long. When I Import the file with access manually goes fast. But how to use this fromout a C# programme who has done this before and who can give met some answers
8
6299
friendjin
by: friendjin | last post by:
How can I read every line from the existed file. eg. the content of textfile: serialnumber = " 50"; serialfile path = "C:\\doc\\"; errorfile path = "C:\\doc\\d\\"; I want to read "50" from the first line to SeNo, read ""C:\\doc\\ "from the second line to SePath, read "C:\\doc\\d\\" ifrom the third line to ErPath. any idea how it can be achieved. thanks
11
3359
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is called that line of info does not exist anymore in the file. for example i have created a short example of where i am at, but I dont know how to alter what im doing so that i just read in a single line
6
32923
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line). Is there some way to burst read the whole file and later "extract" each line? Thanks in advance, Thomas Kowalski
4
3238
by: aarthi28 | last post by:
Hi, I am trying to read every other string from a text file and store it in a vector. I can use istream_iterator to read all strings and store it in a vector. However, I only need the first, third,5th etc string in a vector. How do I do this? Thanks a lot. Any help I can get is appreciated
5
5775
by: yogi_bear_79 | last post by:
I have a tab delimited file, that I want to read into a 2D array. I want to ignore the first two lines and the first colum of data.
2
4303
by: Matt F | last post by:
I am trying to read a text file and store each line as a seperate entry in an array. I have tried searching for how to do this but I can only find how to store the whole file into a 1D array.
4
1960
by: kassik | last post by:
Hi I'm doing a little c++ project. I have a notepad file and I would like to read each line into a separate array of integers.Suppose I have a text file that looks like this: 001-1 1/4 002-2 1/32 010-1 -1/1 011-2 -3/8 01-21 1/4 01-10 1/2 020-2 -1/8
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.