[Please don't top-post]

Rich Busse wrote:
> 
> From: John W. Krahn [mailto:[EMAIL PROTECTED]] 
> >
 > > Here is one way to do it:
> >
 > > $_ = q[P=IcwRcsm D=D: SL=20 ST=d:\icw\rcsm\StartSv.bat Parm1 Parm2
>  > U=http://uslv...];
> >
 > > my ( $Proc, $Start, $Url, $Sleep, $Drive );
>  > my %keys;
> >
 > > @keys{ qw/P ST U SL D/ } = \( $Proc, $Start, $Url, $Sleep, $Drive );
> >
 > > while ( /(\S+)=(.+?)(?=\s+\S+=|\z)/g ) {
>  >     ${$keys{uc $1}} = $2;
>  >     }
> >
 > > print "Proc: $Proc\nStart: $Start\nUrl: $Url\nSleep: $Sleep\nDrive:
>  > $Drive\n";
 >
 > Thank you, John. This code does exactly what I want. Problem is, I
 > only understand about 30% of what's going on. I can figure out
 > the use of the hash, some of the pattern matching & $1/$2. But can
 > someone elaborate on:
 >
 > @keys{ qw/P ST U SL D/ } = \( $Proc, $Start, $Url, $Sleep, $Drive );

This populates the hash %keys with the letters to search for as the keys 
and references to the variables declared earlier as the values.  It is 
equivalent to:

my %keys = (
         P  => \$Proc,
         ST => \$Start,
         U  => \$Url,
         SL => \$Sleep,
         D  => \$Drive,
         );


 > /(\S+)=(.+?)(?=\s+\S+=|\z)/g

This searches for one or more non-whitespace characters (\S+ captured in 
$1) followed by an equal sign (=) followed by one or more non-newline 
characters (.+? captured in $2).  This pattern must be followed by a 
whitespace, non-whitespace, equal-sign (\s+\S+=) pattern or the end of 
the string (\z) but the pattern enclosed in (?=pattern) does not affect 
where the search looks for the second and subsequent matches.


 > ${$keys{uc $1}} = $2;

Since the values of %keys are references to scalar variables we have to 
dereference them to store the content.  This is equivalent to:

${ \$Drive } = $2 if uc $1 eq 'D';


N.B.  After I posted I thought of this change to make it more robust:
while ( /(\S+)=(.+?)(?=\s+\S+=|\z)/g ) {
     ${$keys{uc $1}} = $2 if exists $keys{uc $1};
     }


HTH

John
-- 
use Perl;
program
fulfillment


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to