Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Hash problem

by Nesh (Beadle)
on May 19, 2005 at 02:15 UTC ( [id://458490]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Monks I have a text file as
[17/May/2005:18:15:59 -0700]sdjhsj USERID=t70xvcy; jdhdh JSESSIONID=00 +00RDnYTABcRWOx0UN2Zq--sZB:10f408hdg [17/May/2005:18:15:59 -0700]sdjhsj USERID=t70xvcy; jdhdh JSESSIONID=00 +00RDnYTABcRWOx0UN2Zq--sZB:10f408hdg [17/May/2005:18:15:59 -0700]sdjhsj USERID=t70xvcy; jdhdh JSESSIONID=00 +00RDnYTABcRWOx0UN2Zq--sZB:10f408g13
I have the code as
my $FileName = "test.txt"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(tim +e); open ACCESS,"< $FileName" || die "Can't open the file...."; my %User_Lst; while (<ACCESS>) { if (/:(..):(..):(..).*USERID=([a-z0-9].+; ).*JSESSIONID=([a-z0-9].+)/ +){ $l_hr=$1; $l_min=$2; $l_sec=$3; $UserID=$4; $session=$5; $UserID=~s/;.*//g; $session=~s/.*://g; $User_Lst{$UserID}=join(":",$l_hr,$l_min,$l_sec); # $User_Lst{$sessionid}=$session; } } my $l_Counter=0; #while ( (($UserID,$sessionid $TStamp) = each (%User_Lst)) and (($sess +ionid) = each (%Session_Lst))) { while ( ($UserID, $TStamp) = each (%User_Lst)) { print sprintf("Server Name :%s,\t User Name: %s ,\t Time:%s \n", +$sessionid, $UserID, $TStamp); $l_Counter++; } close ACCESS;
Now the problem I am facing is that how do I push session on to the hash so that I can extract it later in order to populate the result. Appreciate your help.

Replies are listed 'Best First'.
Re: Hash problem
by rupesh (Hermit) on May 19, 2005 at 04:29 UTC

    use strict; my %hash; foreach(<DATA>) { $hash{$1}=() if /JSESSIONID=(.*)$/; } foreach (sort keys %hash) { print $_."\n"; } __DATA__ [17/May/2005:18:15:59 -0700]sdjhsj USERID=t70xvcy; jdhdh JSESSIONID=00 +00RDnYTABcRWOx0UN2Zq--sZB:10f408hdg [17/May/2005:18:15:59 -0700]sdjhsj USERID=t70xvcy; jdhdh JSESSIONID=00 +00RDnYTABcRWOx0UN2Zq--sZB:10f408hdg [17/May/2005:18:15:59 -0700]sdjhsj USERID=t70xvcy; jdhdh JSESSIONID=00 +00RDnYTABcRWOx0UN2Zq--sZB:10f408g13


    Cheers,
    Rupesh.
      I am amazed that
      $hash{$1}=()
      works without warning, on 5.6.1 and on 5.8.4, as does
      my $x = ();

      Can anybody explain, are my expectations so off?

        From perldata:

        If you evaluate an array in scalar context, it returns the length
        of the array. (Note that this is not true of lists, which return the last
        value, like the C comma operator, nor of built-in functions, which
        return whatever they feel like returning.)

        An empty list doesn't have any values, so trying to get the last value gives you undef.


        We're not surrounded, we're in a target-rich environment!
Re: Hash problem
by jdporter (Paladin) on May 19, 2005 at 04:37 UTC
    So you want to report the user, session id, and timestamp of each record grouped by user? Could be tricky, since even in your very brief sample data, none of those fields is uniquely occurring. So, let's try listing by user, primarily, and by time, secondarily. Any complete duplicates are not shown; just one instance will be printed. (However, the count is known, if you decide to print that too.)
    my %User_Lst; open ACCESS,"< $FileName" or die "Can't open $FileName for read: $!\n" +; while (<ACCESS>) { /:(..:..:..).*USERID=([a-z0-9].+); .*JSESSIONID=[a-z0-9].*?:(.+)/ and $User_Lst{$2}{$1}{$3}++; } close ACCESS; for my $UserID ( sort keys %User_Lst ) { for my $TStamp ( sort keys %{ $User_Lst{$UserID} } ) { for my $session ( sort keys %{ $User_Lst{$UserID}{$TStamp} } ) { printf "Server Name :%s,\t User Name: %s ,\t Time:%s \n", $session, $UserID, $TStamp; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://458490]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-18 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found