Reading lines from a file

Discussion in 'Perl Misc' started by tux.tango@gmail.com, Feb 12, 2009.

  1. Guest

    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
    ====
     
    , Feb 12, 2009
    #1
    1. Advertising

  2. J. Gleixner Guest

    wrote:
    > 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;
    }






    >
    >
    > #!/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. Gleixner, Feb 12, 2009
    #2
    1. Advertising

  3. "" <> wrote in news:34eb30e8-17bd-
    :

    > 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 <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Feb 13, 2009
    #3
  4. "" <> wrote in news:34eb30e8-17bd-
    :

    <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 <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Feb 13, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,710
  2. Joe Wright
    Replies:
    0
    Views:
    541
    Joe Wright
    Jul 27, 2003
  3. lovecreatesbeauty

    How to know two lines are a pare parallel lines

    lovecreatesbeauty, Apr 27, 2006, in forum: C Programming
    Replies:
    11
    Views:
    682
    Old Wolf
    Apr 28, 2006
  4. Replies:
    1
    Views:
    471
    Jonathan Mcdougall
    Dec 6, 2005
  5. Murali
    Replies:
    2
    Views:
    595
    Jerry Coffin
    Mar 9, 2006
Loading...

Share This Page