Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Loop through a text file line by line

Reply
Thread Tools

Loop through a text file line by line

 
 
toomanyjoes@mail.utexas.edu
Guest
Posts: n/a
 
      01-13-2005
Hi,

I'm trying to loop through a .txt file and grab a number at the
beginning of the line. I want to save this number in a variable and
then compare it with the number at the beginning of the next line. For
instance my text would look like this:

1sampletextsampletextsamplet22tsampletextsampletex tsampletext
2sampletextsampletextsamplet22tsampletextsampletex tsampletext
3sampletextsampletextsamplet22tsampletextsampletex tsampletext

The lines are distinct from one other, each one ends with a newline
character. How do I loop through a text file line by line?

#Heres my code so far

$myvar = "1sampletextsampletextsamplet22tsampletext"; #easy way out of
opening the text file (I'm not sure how)
if ($myvar =~ /^(\d+)/){
$curNum = "$1"; #curNum should equal 1
}
print "$curNum";

This only works for an individual line. I need to move to the next line
and do the same thing, then compare $curNum with $newNum.

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      01-13-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> I'm trying to loop through a .txt file and grab a number at the
> beginning of the line.
>


From the command line:

perldoc perlopentut

Or try:

http://perldoc.com/perl5.8.4/pod/perlopentut.html

Matt


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

> I'm trying to loop through a .txt file and grab a number at the
> beginning of the line. I want to save this number in a variable and
> then compare it with the number at the beginning of the next line.



> The lines are distinct from one other, each one ends with a newline
> character.



You should speak Perl rather than English, when possible,
(as suggested in the Posting Guidelines) then you won't need
to try and explain it in English.

foreach ( "1sampletextsampletextsamplet22tsampletextsamplete xtsampletext\n",
"2sampletextsampletextsamplet22tsampletextsamplete xtsampletext\n",
"3sampletextsampletextsamplet22tsampletextsamplete xtsampletext\n"
) {


> How do I loop through a text file line by line?



By using a while() loop (see the "Compound statements"
section in perlsyn.pod), and the Input operator (see the
"I/O Operators" section in perlop.pod).


> $myvar = "1sampletextsampletextsamplet22tsampletext"; #easy way out of



Errr, I thought you said the strings had newlines on the end.

That string does NOT have a newline at the end.


There should be a "my " at the beginning of that line.

You _are_ enabling strict (as suggested in the Posting Guidelines),
aren't you?


> opening the text file (I'm not sure how)



You use the open() function to open a file.

perldoc -f open


If you use the __DATA__ token, then you can post file contents
as part of your program, as suggested in the Posting Guidelines
(that you have read so many times).


> if ($myvar =~ /^(\d+)/){
> $curNum = "$1"; #curNum should equal 1
> }
> print "$curNum";
>
> This only works for an individual line. I need to move to the next line
> and do the same thing, then compare $curNum with $newNum.



Errr, what is $newNum? It does not appear in your code anywhere...

What kind of comparison do you need?

Equal/not equal to the previous?
one more than the previous?
larger than the previous?
smaller than the previous?
....


---------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $curNum;
while ( my $myvar = <DATA> ) {
$curNum = $1 if $myvar =~ /^(\d+)/;
}
print "$curNum\n";

__DATA__
1sampletextsampletextsamplet22tsampletextsampletex tsampletext
2sampletextsampletextsamplet22tsampletextsampletex tsampletext
3sampletextsampletextsamplet22tsampletextsampletex tsampletext
---------------------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-13-2005
(E-Mail Removed) wrote in news:1105583533.605587.261600
@z14g2000cwz.googlegroups.com:

> The lines are distinct from one other, each one ends with a newline
> character. How do I loop through a text file line by line?


perldoc -f readline

Sinan.


--
A. Sinan Unur
(E-Mail Removed)d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      01-13-2005
(E-Mail Removed) wrote :
>
> I'm trying to loop through a .txt file and grab a number at the
> beginning of the line. I want to save this number in a variable and
> ...


Have you read my posting in your other thread?
Take a look at the "sub printline", which does exactly what you are
asking for.

>
> The lines are distinct from one other, each one ends with a newline
> character. How do I loop through a text file line by line?
>


This is done within the while loop in the other posting.
If you replace <DATA> with <> you can read from STDIN.
You could also open a File with th open functioni (perldoc -f open).

HTH

--
Epur Si Muove (Gallileo Gallilei)
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      01-14-2005
On 13 Jan 2005 10:20:57 GMT, Martin Kissner <(E-Mail Removed)> wrote:

>This is done within the while loop in the other posting.
>If you replace <DATA> with <> you can read from STDIN.


Not exactly. From 'perldoc perlop':

| The null filehandle <> is special: it can be used to emulate the
| behavior of sed and awk. Input from <> comes either from standard input,
| or from each file listed on the command line. Here's how it works: the
| first time <> is evaluated, the @ARGV array is checked, and if it is
| empty, $ARGV[0] is set to "-", which when opened gives you standard
| input. The @ARGV array is then processed as a list of filenames. The
| loop


HTH,
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      01-14-2005
Michele Dondi wrote :
> On 13 Jan 2005 10:20:57 GMT, Martin Kissner <(E-Mail Removed)> wrote:
>
>>This is done within the while loop in the other posting.
>>If you replace <DATA> with <> you can read from STDIN.

>
> Not exactly. From 'perldoc perlop':
> [...]


In <(E-Mail Removed)> Arndt wrote to me:
| The "diamond" operator <> is described in perlop and perlopentut.
| You read from STDIN by using the normal Unix syntax:
|
| ./myperlscript.pl < file

This worked for me.

--
Epur Si Muove (Gallileo Gallilei)
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      01-14-2005
On 14 Jan 2005 10:24:09 GMT, Martin Kissner <(E-Mail Removed)> wrote:

>> Not exactly. From 'perldoc perlop':
>> [...]

>
>In <(E-Mail Removed)> Arndt wrote to me:
>| The "diamond" operator <> is described in perlop and perlopentut.
>| You read from STDIN by using the normal Unix syntax:
>|
>| ./myperlscript.pl < file
>
>This worked for me.


So what?!?


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      01-14-2005
Michele Dondi wrote :
> On 14 Jan 2005 10:24:09 GMT, Martin Kissner <(E-Mail Removed)> wrote:
>
>>> Not exactly. From 'perldoc perlop':
>>> [...]

>>
>>In <(E-Mail Removed)> Arndt wrote to me:
>>| The "diamond" operator <> is described in perlop and perlopentut.
>>| You read from STDIN by using the normal Unix syntax:
>>|
>>| ./myperlscript.pl < file
>>
>>This worked for me.

>
> So what?!?


I said: "If you replace <DATA> with <> you can read from STDIN."
You said: "Not exactly. ..."
I was not sure if I have expressed myself correctly so tried to clear
up what I meant.

--
Epur Si Muove (Gallileo Gallilei)
 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      01-14-2005
Michele Dondi wrote :
> On 14 Jan 2005 10:24:09 GMT, Martin Kissner <(E-Mail Removed)> wrote:
>
>>> Not exactly. From 'perldoc perlop':
>>> [...]

>>
>>In <(E-Mail Removed)> Arndt wrote to me:
>>| The "diamond" operator <> is described in perlop and perlopentut.
>>| You read from STDIN by using the normal Unix syntax:
>>|
>>| ./myperlscript.pl < file
>>
>>This worked for me.

>
> So what?!?


I was not sure if I have expressed myself correctly so tried to clear
up what I meant.

--
Epur Si Muove (Gallileo Gallilei)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Reading LAST line from text file without iterating through the file? Robin Wenger Java 191 03-26-2011 06:19 PM
loop through each line in a text file qtrimble Python 6 03-01-2010 04:15 PM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
reading a line by line from local text file through WATIR curious Ruby 1 10-25-2006 01:34 PM



Advertisments