Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] A File PArse Beginner Qeustion

Expand Messages
  • Charles K. Clarkson
    ... perl keeps track of line numbers in a file using $. In a while block, ($. % 4) will fail every fourth line. So, push @records, $_; next if $. % 4; will
    Message 1 of 3 , Mar 30, 2002
    • 0 Attachment
      "Ofir" <ofirb1@...> asked:

      : Now, what is the routine for parsing a simpler file, i.e:
      : file with a four-line record, WITHOUT the blank line which separate the
      : records?
      : i.e:
      : Line 1
      : Line 2
      : Line 3
      : Line 4
      : Line 1
      : Line 2
      : Line 3
      : Line 4
      : I know it's basic, but I appreciate any help!

      perl keeps track of line numbers in a file using $. In a
      'while' block, ($. % 4) will fail every fourth line. So,

      push @records, $_;
      next if $. % 4;

      will fall through with 4 more records each time.

      use Data::Dumper;

      print Dumper get_records('new.txt');

      sub get_records {
      my $file_name = shift;
      local *FH;
      open FH, $file_name or die "Cannot open $file_name: $!";
      my( @records, @news );
      while ( <FH> ) {
      chomp;
      push @records, $_;
      next if $. % 4;

      my %news;
      @news{ qw/url date news source/ } = @records;
      push @news, \%news;
      @records = ();
      }

      return @news;
      }


      HTH,

      Charles K. Clarkson
      --
      Clarkson Energy Homes, Inc.
      CJ Web Work - Domains for Real Estate Investors.

      E Pluribus Unum -- One from many.
    Your message has been successfully submitted and would be delivered to recipients shortly.