Jay Savage wrote:
> On 10/4/06, John W. Krahn <[EMAIL PROTECTED]> wrote:
>> Michael Alipio wrote:
>>
>> > Now, my goal is to adapt that code, particularly
>> > obtaining only Start, IP, User. However, those three
>> > targets are not anymore located at the beginning of a
>> > line.
>> >
>> > "Start" is the date=.time= combination,
>> > "IP" is found after rem_ip=
>> > "User" is found after "user: "
>> >
>> > I'm not really sure how to put my regexp inside my
>> > hash..
>> >
>> > while ( <LOGFILE> ) {
>> > my %extr = (
>> > Start => '',
>> > IP => '',
>> > User => '',
>> > /what should i put here??/mg
>> > );
>> > print "Start:$extr{Start} IP:$extr{IP}
>> > User:$extr{User}\n\n";
>> > }
>>
>> You don't really need a hash,
>
> Probably not, but can someone explain what's going on here? It looks
> to me like the code creates a list of k/v pairs to initialize some
> hash keys with empty values, and then continues the list with a series
> of k/v pairs, returned from the match captures, which immediately
> overrides the vlaues for the keys that were just declared. In other
> words, there are two assignments for the same keys in the same list?
> Or is there some magic that happens when a hash is passed a regex in
> list context so that the assignments really only happen once?
> Wouldn't
>
> my %extr = (/^(Start|IP|User|End)=(.+)/mg);
>
> on its own achieve the same result as
>
> my %extr = (
> Start => '',
> IP => '',
> User => '',
> End => '',
> /^(Start|IP|User|End)=(.+)/mg
> );
>
> Or am I missing something?
If one (or more) of the keys is missing, say 'User', and you print
"User:$extr{User}" you will get a warning. This way all the values will have
a string value and there will be no warning.
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>