frank_2k4 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings perl monks, I couldn't find an answer to my question, so hoping I can get some help:
I have some output that looks like: afp:usersArray:_array_index:0:serviceType = "afp" afp:usersArray:_array_index:0:ipAddress = "1.1.1.1" afp:usersArray:_array_index:0:sessionID = 235 afp:usersArray:_array_index:0:loginElapsedTime = 2796 afp:usersArray:_array_index:0:disconnectID = 0 afp:usersArray:_array_index:0:name = "admin" afp:usersArray:_array_index:0:flags = 1 afp:usersArray:_array_index:1:serviceType = "afp" afp:usersArray:_array_index:1:ipAddress = "1.1.1.1" afp:usersArray:_array_index:1:sessionID = 244 afp:usersArray:_array_index:1:loginElapsedTime = 2286 afp:usersArray:_array_index:1:disconnectID = 0 afp:usersArray:_array_index:1:name = "admin" afp:usersArray:_array_index:1:flags = 1 afp:timeStamp = "2011-03-17 16:45:37 -0700"
The goal is to get a username, and sessionid. Also need to associate this with its array index. Here is the code:
#-Code getList(); for (keys %uhash){ print "Final: ArrayNum:$_ and SessionName: $uhash{$_}->{sessionName} + SessionId:$uhash{$_}->{sessionId}\n"; } #-Subs sub getList { open FH,"output"; while (<FH>){ if ($flag{debug}){ print "DEBUG: OUTPUT: $_\n"; } if (/.*array_index:(\d+):serviceType.*/){ $arrayNum = $1; if ($flag{debug}){ print "DEBUG:ArrayNum: $1\n"; } } unless(!$arrayNum){ if (/.*array_index:$arrayNum:sessionID = (\d+)/){ $sessionId = "$1"; $uhash{$arrayNum}->{sessionId} = $sessionId; if ($flag{debug}){ print "DEBUG:SessionId: $sessionId\n"; } } if (/.*array_index:$arrayNum:name = "(\w+)"/){ $sessionName = "$1"; $uhash{$arrayNum}->{sessionName} = $sessionName; if ($flag{debug}){ print "DEBUG:SessionName: $sessionName\n"; } } } } }
Output I get is:
Final: ArrayNum:1 and SessionName: admin SessionId:244
As you can see I am missing array index 0 data. Any help is much appreciated. I need to find a way to run multiple regex against a single file. Thank you in advance!

Replies are listed 'Best First'.
Re: while loop and multiple regex
by jwkrahn (Abbot) on Mar 18, 2011 at 00:58 UTC
    unless(!$arrayNum){

    I am missing array index 0 data.

    That is because your code will skip processing when $arrayNum is equal to zero because zero is FALSE.

      Thanks for your help. I feel like such a dunce for missing this. Thanks for saving me hours before figuring it out.. ~~~!!!!