SPUG: reg exp's

Yitzchak Scott-Thoennes sthoenna at efn.org
Tue Sep 3 17:31:02 CDT 2002


On Tue, 3 Sep 2002 10:36:06 -0700 , stephen.m.baker at intel.com wrote:
>I need to parse quickly through a file of the following format:
[snip]
>to populate a data structure.  Is regular expressions the way to go about
>this? OR does somebody have a trick for how this might be done?

You don't say what the data structure should look like, but
you might try something like this:
==============================
use strict;
use warnings;

my $data;
$/ = '';  # read in a paragraph at a time
while (<DATA>) {
    push @$data, { split /:\s*(.*)\n+/ }
}

use Data::Dumper;
print Dumper($data);
__DATA__
heading1: data1.1
heading2: data2.1
heading3: data3.1

heading1: data1.2
heading2: data2.2
heading3: data3.2

heading1: data1.3
heading2: data2.3
heading3: data3.3
==============================

(I hope you aren't a consultant for Intel...)

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list