in reply to Finding installed program version in Win32 registry
There is a useful technique for dealing with this kind of variably ordered input. It revolves around placing captures inside lookahead assertions.
Because the lookahead assertions do not alter pos, the captured fields are placed into the capture variables ($1, $2, $3 ... ), in the same order, regardless of the order in which that information appears in the input. This is very useful as it greatly simplifies the subsequent processing.
The following example I've re-ordered the two fields of interest in three copies of the sample data, and the output shows that the two fields (DisplayName & DisplayVersion) have been assigned to $1 & $2 respectively, regarless of their relative positioning in the input record.
This demo use "paragraph mode", ($/ = '';) which assumes that the records are separated by one or more blank lines:
#! perl -sw use 5.010; use strict; $/ = ''; ## Paragraph mode while( <DATA> ) { m[ (?= ^ .*? DisplayName \s+ \S+ \s+ ( [^\n]+ ) \n ) (?= ^ .*? DisplayVersion \s+ \S+ \s+ ( [^\n]+ ) \n ) ]xsm and say "Name: $1 Version $2"; }
Output:
c:\test>755675.pl Name: Adobe Reader 8 Version 8.0.0 Name: Adobe Reader 8 Version 8.0.0 Name: Adobe Reader 8 Version 8.0.0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Finding installed program version in Win32 registry
by 1Nf3 (Pilgrim) on Apr 06, 2009 at 11:46 UTC | |
by BrowserUk (Patriarch) on Apr 06, 2009 at 12:01 UTC |