Re: reading text data to variable
If you want to process lines in a file twice, there are two simple choices: 1. Save the lines in a array the first time you read the file, then traverse the array as many times as you want. This works unless you have a humungous file. 2. Call seek(,0,0) on the file handle and read the file again. > On Dec 26, 2018, at 9:40 AM, Eldon DeHart wrote: > > I can't figure out how to read each line of text back into my program and > assign it to the variable again. > Thanks for help in advance. > Eldon > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ Jim Gibson jimsgib...@gmail.com -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: reading text data to variable
On 12/26/18 12:40 PM, Eldon DeHart wrote: I can't figure out how to read each line of text back into my program and assign it to the variable again. Thanks for help in advance. Eldon please show us the code you have and then we can better help guide you. also read the FAQ as that is likely covered in it. uri
reading text data to variable
I can't figure out how to read each line of text back into my program and assign it to the variable again. Thanks for help in advance. Eldon wbws.pl Description: Perl program -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading text file in reverse order
Mame Mbodji wrote: This is a hwk, but I never asked for a complete answer 1. The term homework refers to something fluid, rather than discrete, and therefore does not take an article ['a' or 'the'] 2. Please do not abbreviate words, unless the abbreviation is a very standard one. I wasted 2-3 minutes just puzzling out what hwk meant: Hawk? some mispspelling or typographical error/ Who knows? There are enough accidental errors that get into e-mail traffic. There is no need to add those that arise simply from a lack of effort. When asking for guidance related to school studies, it is a good idea to explain what topics you are covering. The example problem you are working with can be handled in a number of ways. Which one may be most appropriate depends largely on what programming tools it is intended to help you learn about. Is the reverese function the focus of your studies? Have shoft or unshift come up in your class readings? Is the focus on handling string data? One thing I would suggest from seeing your code--you need to have more respect for the material. Int his code: while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } You use some variant or abbreviation for the word line in three different places. You do not use them sensibly. The plural of line is lines. This should probably be the name of the array, since it is the array that holds more than one line. The elements in each line are characters, not lines. Why do you call them @lines? That doesn't make sense. There are a number of simple ways to achieve the desired effect. Please rework your code, using variable names that indicate that you have put some serious thought into the problem, and repost. We can probably guide you towards some good solutions. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
[EMAIL PROTECTED] wrote: I have a text file with 5 or more lines as in: My name is this and that I live in Denver Colorado I live in washington I live in Denver Virginia I am trying to read this file and print each line and each word in reverse order as in: ainigriv revned ni evil I (the output starts with the last last of the file and reverse each work as well). Here is what I have done, but does not work: print Enter a file name:\n; my $file = STDIN; open (REGFILE, $file) or die (Cannot open file: $file: $!); while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } use File::ReadBackwards; tie *FILE, 'File::ReadBackwards', $file or die Cannot open $file: $!; while ( FILE ) { chomp; print scalar reverse, \n; } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
Thanks for the advice, I will try HARDER and repost if I cannot figure it out! R. Joseph Newton wrote: Mame Mbodji wrote: This is a hwk, but I never asked for a complete answer 1. The term homework refers to something fluid, rather than discrete, and therefore does not take an article ['a' or 'the'] 2. Please do not abbreviate words, unless the abbreviation is a very standard one. I wasted 2-3 minutes just puzzling out what hwk meant: Hawk? some mispspelling or typographical error/ Who knows? There are enough accidental errors that get into e-mail traffic. There is no need to add those that arise simply from a lack of effort. When asking for guidance related to school studies, it is a good idea to explain what topics you are covering. The example problem you are working with can be handled in a number of ways. Which one may be most appropriate depends largely on what programming tools it is intended to help you learn about. Is the reverese function the focus of your studies? Have shoft or unshift come up in your class readings? Is the focus on handling string data? One thing I would suggest from seeing your code--you need to have more respect for the material. Int his code: while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } You use some variant or abbreviation for the word line in three different places. You do not use them sensibly. The plural of line is lines. This should probably be the name of the array, since it is the array that holds more than one line. The elements in each line are characters, not lines. Why do you call them @lines? That doesn't make sense. There are a number of simple ways to achieve the desired effect. Please rework your code, using variable names that indicate that you have put some serious thought into the problem, and repost. We can probably guide you towards some good solutions. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
Thank you so that. This is the kind of advice I have been expecting. This is very helpful and I think I will be able to figure it out now. I never intended for the group to do my HMW. I have been with this list for over a year and I learned a lot, but I never posted a question related to my homework. Randy W. Sims wrote: On 3/11/2004 11:05 PM, Mame Mbodji wrote: This is a hwk, but I never asked for a complete answer. I just needed guidance because I was lost. Thank you anyway. Did you understand the hint I gave earlier? Your code while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } is equivelant to: my @line = REGFILE; foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } because @line gets the whole file at one time, so the while loop only loops once. (put a print statement just after the while line to see for yourself.) The variable @line should probably be named @lines (plural) because the array contains all lines in the file; each element in the array is a line in the file. my @lines = REGFILE; If you want the last line first and the first line last you'll need to reverse the elements in that array... Next you'll need to iterate over each line and break up the string of characters into an array so you can reverse them. There are several ways to break a string into an array of characters (unpack,substr,split), but the common idiom in this case is to use split. The documentation for split will tell you exactly how to split the string into an array of characters. Let us know if you need more hints. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
John W. Krahn wrote: [EMAIL PROTECTED] wrote: I have a text file with 5 or more lines as in: My name is this and that I live in Denver Colorado I live in washington I live in Denver Virginia I am trying to read this file and print each line and each word in reverse order as in: ainigriv revned ni evil I (the output starts with the last last of the file and reverse each work as well). Here is what I have done, but does not work: print Enter a file name:\n; my $file = STDIN; open (REGFILE, $file) or die (Cannot open file: $file: $!); while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } use File::ReadBackwards; tie *FILE, 'File::ReadBackwards', $file or die Cannot open $file: $!; while ( FILE ) { chomp; print scalar reverse, \n; } __END__ Looks cool, and perfectly tailored to the task. How is it for handligg muti-charcter newlines? Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
R. Joseph Newton wrote: John W. Krahn wrote: use File::ReadBackwards; tie *FILE, 'File::ReadBackwards', $file or die Cannot open $file: $!; while ( FILE ) { chomp; print scalar reverse, \n; } __END__ Looks cool, and perfectly tailored to the task. How is it for handligg muti-charcter newlines? The documentation states that this is handled correctly. perldoc File::ReadBackwards [snip] DESCRIPTION This module reads a file backwards line by line. It is simple to use, memory efficient and fast. It supports both an object and a tied handle interface. It is intended for processing log and other similar text files which typically have their newest entries appended to them. By default files are assumed to be plain text and have a line ending appropriate to the OS. But you can set the input record separator string on a per file basis. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Reading text file in reverse order
I have a text file with 5 or more lines as in: My name is this and that I live in Denver Colorado I live in washington I live in Denver Virginia I am trying to read this file and print each line and each word in reverse order as in: ainigriv revned ni evil I (the output starts with the last last of the file and reverse each work as well). Here is what I have done, but does not work: print Enter a file name:\n; my $file = STDIN; open (REGFILE, $file) or die (Cannot open file: $file: $!); while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } Any ideas! Thanks in advance __ Introducing the New Netscape Internet Service. Only $9.95 a month -- Sign up today at http://isp.netscape.com/register Netscape. Just the Net You Need. New! Netscape Toolbar for Internet Explorer Search from anywhere on the Web and block those annoying pop-ups. Download now at http://channels.netscape.com/ns/search/install.jsp -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
On 3/11/2004 10:01 PM, [EMAIL PROTECTED] wrote: Sorry, no answers. This sounds like a school assignemnt. But I'll offer a few hints ;-) I have a text file with 5 or more lines as in: My name is this and that I live in Denver Colorado I live in washington I live in Denver Virginia I am trying to read this file and print each line and each word in reverse order as in: ainigriv revned ni evil I (the output starts with the last last of the file and reverse each work as well). Here is what I have done, but does not work: always: use strict; use warnings; print Enter a file name:\n; my $file = STDIN; open (REGFILE, $file) or die (Cannot open file: $file: $!); The diamond operator can be used in two different contexts: 1) in scalar context (my $line = ) it reads one line at a time from the file, 2) in array context (my @lines = ) it reads the entire file at once. Below you are reading the entire file into @line at one time. I.e. you could remove the 'while' statement and nothing would change. while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } Any ideas! Thanks in advance See if the above gets you any further along and let us know. Regards, Randy. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
[EMAIL PROTECTED] wrote: I have a text file with 5 or more lines as in: My name is this and that I live in Denver Colorado I live in washington I live in Denver Virginia I am trying to read this file and print each line and each word in reverse order as in: ainigriv revned ni evil I (the output starts with the last last of the file and reverse each work as well). Here is what I have done, but does not work: print Enter a file name:\n; my $file = STDIN; open (REGFILE, $file) or die (Cannot open file: $file: $!); while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } Any ideas! Thanks in advance I am sure there is a better way... print Enter a file name: ; chomp(my $file = STDIN); open (REGFILE, $file) or die (Cannot open file: $file: $!); while(my @line = REGFILE) { foreach $lin(@line){ @lines = split( , $lin); $revwords = join( , reverse @lines); print $revwords,\n; } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
Randy W. Sims wrote: On 3/11/2004 10:01 PM, [EMAIL PROTECTED] wrote: Sorry, no answers. This sounds like a school assignemnt. But I'll offer a few hints ;-) I have a text file with 5 or more lines as in: My name is this and that I live in Denver Colorado I live in washington I live in Denver Virginia I am trying to read this file and print each line and each word in reverse order as in: ainigriv revned ni evil I (the output starts with the last last of the file and reverse each work as well). Here is what I have done, but does not work: always: use strict; use warnings; print Enter a file name:\n; my $file = STDIN; open (REGFILE, $file) or die (Cannot open file: $file: $!); The diamond operator can be used in two different contexts: 1) in scalar context (my $line = ) it reads one line at a time from the file, 2) in array context (my @lines = ) it reads the entire file at once. Below you are reading the entire file into @line at one time. I.e. you could remove the 'while' statement and nothing would change. while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } Any ideas! Thanks in advance See if the above gets you any further along and let us know. Regards, Randy. Doh! I hope it wasn't his assignment! As a newbie myself it took me about 10 minutes to do that. Robert -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
Doh! I hope it wasn't his assignment! As a newbie myself it took me about 10 minutes to do that. That's ok. Your solution is the wrong solution ;-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
This is a hwk, but I never asked for a complete answer. I just needed guidance because I was lost. Thank you anyway. Randy W. Sims wrote: Doh! I hope it wasn't his assignment! As a newbie myself it took me about 10 minutes to do that. That's ok. Your solution is the wrong solution ;-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reading text file in reverse order
On 3/11/2004 11:05 PM, Mame Mbodji wrote: This is a hwk, but I never asked for a complete answer. I just needed guidance because I was lost. Thank you anyway. Did you understand the hint I gave earlier? Your code while(my @line = REGFILE) { foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } } is equivelant to: my @line = REGFILE; foreach my $lin(@line){ my @lines = $lin; print reverse(@lines); } because @line gets the whole file at one time, so the while loop only loops once. (put a print statement just after the while line to see for yourself.) The variable @line should probably be named @lines (plural) because the array contains all lines in the file; each element in the array is a line in the file. my @lines = REGFILE; If you want the last line first and the first line last you'll need to reverse the elements in that array... Next you'll need to iterate over each line and break up the string of characters into an array so you can reverse them. There are several ways to break a string into an array of characters (unpack,substr,split), but the common idiom in this case is to use split. The documentation for split will tell you exactly how to split the string into an array of characters. Let us know if you need more hints. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
reading text file
Hi All, I am a newbee to CGI. I have about 100 textfiles with particulars as Name: Employee_Number: Department: Area: etc in each file. The data here is about individual employees stored in separate text files on the server. On the homepage I will be giving a list of all the names of the employees. And upon a click of the link, of an individual name, I wish to show an HTML page which uses a cgi script to read the corresponding employee text file and load the information and display in the browser. I am looking for some samples which have similer function and that I can modify it to suit to my needs. I shall greatly appreciate if some one can help proceed in achieving this. Thanks Mike __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Reading text
On Tue, 29 Jan 2002 21:56:38 -0600, Michael Pratt wrote: What is the simplest means of reading a text file line by line. #!/usr/bin/perl -w use strict; open(FILE, ' file.txt') or die Cannot open file: $!\n; while(FILE){ # Read each line of file.txt until end of file. # the current line content is in $_ print Current line: $_; } __END__ -- briac dynamic .sig on strike, we apologize for the inconvenience -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Reading text
What is the simplest means of reading a text file line by line. Thanks! Mike -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]