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!! -
open (FILE, "profile.txt") || die "Can't open File.txt: $!\n";
-
@raw_data=<FILE>;
-
close(FILE);
-
-
# my array only loads last name in file
-
foreach $_ (@raw_data) #foreach loop to grab wanted variables
-
{
-
($c_name, $descript_info)=split(/\|/, $_); #split method create pipe used escaped with \ char two variables one for the names other for numbers
-
-
}
-
sub go{
-
$user= "Please type the name $c_name \n";}
-
MainLoop;
-
-
sub enter{
-
if ($E) #check user input value
-
{ goto contin1;}
-
-
else
-
{$user= "You didn't type anything!!\n";
-
}
-
-
contin1:
-
$user= "\e[H\e[J"; #clear the screen
-
if ( $c_name =~ /$E/ ) #compare with user input
-
{
-
$user="The name -$c_name to $desricpt_info \n";
-
my $mw_next=$main->messageBox(-message => "Remember!",-=>\&go, -type =>$user);
-
}
-
else
-
{
-
$user= "The name was mistyped! Please try again.\n";
-
}
-
First, ALWAYS use the code tags when posting your code.
Add these 2 pragmas and fix the problems they point out. - use strict;
-
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.
14 16694 RonB 589
Expert Mod 512MB
First, ALWAYS use the code tags when posting your code.
Add these 2 pragmas and fix the problems they point out. - use strict;
-
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.
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: -
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).
I hope this helps!
RonB 589
Expert Mod 512MB
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.
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.
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.
Sorry about the missing code tags.
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.
RonB 589
Expert Mod 512MB
We can't help you troubleshoot code that you haven't shown and haven't properly explained the problem.
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. -
-
require Tk;
-
use Tk ':eventtypes';
-
use Tk;
-
-
my $mw = MainWindow->new();
-
-
open (FILE, "new.txt") || die "Can't open File.txt: $!\n";
-
-
$mw->Button (-text=>"names",
-
-command=>[\&printstrings,$_])
-
->pack(-side=>"left");
-
-
$mw->Label(-textvariable=>\$user)->pack();
-
sub printstrings
-
{open (FILE, "new.txt") || die "Can't open File.txt: $!\n";
-
-
foreach $_ (<FILE>)
-
{
-
($c_name, $phone_info)=split(/\|/, $_);
-
$user= "the word $c_name $phone_info\n";
-
}}
-
-
MainLoop();
-
-
RonB 589
Expert Mod 512MB
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: - $user= "the word $c_name $phone_info\n";
To this: - $user .= "the word $c_name $phone_info\n";
Thanks RonB for the insight but how to get it to display each name after I press the button?
RonB 589
Expert Mod 512MB
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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;...
|
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...
|
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...
|
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...
|
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)....
|
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,...
|
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.
|
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.
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |