On Fri, Apr 20, 2001 at 05:00:37PM -0500, Sean C. Phillips wrote:
: Gurus,
: 
: This was very timely and helpful.  I've got another related question,
: hence the reply to this.  What if I've got a stanza like:
: 
: parm  : parm1
: value : 100
: 
: parm  : parm2
: value : 101 
: 
: ... etc, and I want to keep the name of the parm and the value, and
: trash the rest?

Here is an example of what you want, hopefully documented well enough:

#!/usr/bin/perl -w
use strict;
$|++;

$/ = "";  # turn on paragraph slurping, see perlvar

my %params; # declare the hash to hold the results.

while ( <DATA> ) {
  # Split a paragraph by newlines or space-colon-space and
  # only grab the second and fourth sections.  ie: grab 'param1'
  # and '100' from the list 'param', 'param1', 'value', '100'.
  # Accomplish this using an array slice.
  my( $key, $value ) = ( split /\n|\s+:\s+/ )[1, 3];

  # save the pair
  $params{$key} = $value;
}


# Verify the results using Data::Dumper, optional.
use Data::Dumper;
print Dumper \%params;

# Store test data in the __DATA__ section of a Perl program
# for this experiment.
__DATA__
parm    : parm1
value   : 100

parm    : parm2
value   : 101

Enjoy!

-- 
Casey West

Reply via email to