SPUG: reg exp's

John Subaykan cansubaykan at hotmail.com
Tue Sep 3 14:40:14 CDT 2002


Once you open the file and assign a filehandle (FH), you can do something 
like this:

while (<FH>) {

	next unless /\S/;  # skip empty lines

	my ($one,$two) = split /\s*:\s*/;

	chomp($two); # trailing space;

	print "ONE: $one TWO: $two\n";

	# now you can do whatever you want with $one and $two
	# like make a hash $data{$one} = $two  etc..

}


if you want to capture 3 elements like 'heading1' and 'data1' and '1' :


while (<FH>) {

	next unless /\S/;


	# this is general, you can make it more specific by including
	# literal values like 'heading' or 'data'

	my ($h, $d, $n) = /(\w+)\s*:\s*(\w+)\.(\w+)/;

	print "H: $h  D: $d  N: $n\n";


}


you can do more complicated pattern matching as needed, based on your data.

Hope that helps.





----Original Message Follows----
From: "Baker, Stephen M" <stephen.m.baker at intel.com>
To: "'spug-list at pm.org'" <spug-list at pm.org>
Subject: SPUG: reg exp's
Date: Tue, 3 Sep 2002 10:36:06 -0700


I need to parse quickly through a file of the following format:

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

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?


  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      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




_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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