Reading lines from a file

T

tux.tango

I have a text file that is somewhat structured. I can get the "first"
line from the file but not the others. I am not sure how to read all
the lines at the same time. If I check ($count % 8) == 2, I can get
the second lines, but I coundn't figuire out how to check it in the
same while loop.


#!/usr/bin/perl
use strict;

my $count = 0 ;
while(<DATA>) {
$count++ ;
if ( $count % 8 == 1) {
print "Title: $count $_ \n"; }
}


__END__
This is a title
This is a place
This is time
This is Age

This is Desciption
====

This is Title2
This is Place2
This is Time2
This is Age2

This is Description2
====
 
J

J. Gleixner

I have a text file that is somewhat structured. I can get the "first"
line from the file but not the others. I am not sure how to read all
the lines at the same time.

Usually you don't need to do that.

See: File::Slurp

perldoc -q "How can I read in an entire file all at once?"

If I check ($count % 8) == 2, I can get
the second lines, but I coundn't figuire out how to check it in the
same while loop.

Check it how? $_ will contain the information.

I'm not sure what you're after, but the following, might help
show you how to print the lines containing 'title' (ignoring case):

while(<DATA>)
{
print if /title/i;
}
 
A

A. Sinan Unur

I have a text file that is somewhat structured. I can get the "first"
line from the file but not the others. I am not sure how to read all
the lines at the same time. If I check ($count % 8) == 2, I can get
the second lines, but I coundn't figuire out how to check it in the
same while loop.


#!/usr/bin/perl
use strict;

You need use warnings here.
my $count = 0 ;

You do not need $count. Use the special variable $. See perldoc perlvar.
while(<DATA>) {
$count++ ;
if ( $count % 8 == 1) {
print "Title: $count $_ \n"; }
}

I am not sure what you are trying to do. Are you just trying to grab
specific lines? Are you trying to process the file block by block? Have
you seen the posting guidelines?

Is this what you want?

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @data;

{
local $/ = "\n====\n\n";
while ( my $record = <DATA> ) {
chomp $record;
my %item;
@item{ qw( title place time age description )}
= (split /\n/, $record, 6)[0 .. 3, 5];
push @data, \%item;
}
}

print Dumper \@data;


__DATA__
This is a title
This is a place
This is time
This is Age

This is Desciption
====

This is Title2
This is Place2
This is Time2
This is Age2

This is Description2
====

This is Title3
This is Place3
This is Time3
This is Age3

Here is
a multiline
description
====

C:\DOCUME~1\asu1\LOCALS~1\Temp> t
$VAR1 = [
{
'time' => 'This is time',
'place' => 'This is a place',
'title' => 'This is a title',
'description' => 'This is Desciption',
'age' => 'This is Age'
},
{
'time' => 'This is Time2',
'place' => 'This is Place2',
'title' => 'This is Title2',
'description' => 'This is Description2',
'age' => 'This is Age2'
},
{
'time' => 'This is Time3',
'place' => 'This is Place3',
'title' => 'This is Title3',
'description' => 'Here is
a multiline
description',
'age' => 'This is Age3'
}
];

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
A

A. Sinan Unur

(e-mail address removed):

<snip>

Why did you post the same question twice within 16 minutes of each other?

I now see that Tad gave you basically the same answer I did so I wasted my
time.

Bye.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top