#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    21
    Rep Power
    0

    Optimize the display of a result table "Print"


    Hi,
    I'm using following code to print the result and I want a code by using boucle for (i=1 to 10) :


    #print "01h $Parm4_1 $Parm3_1 $Parm1_1 $Parm2_1\n";
    #print "02h $Parm4_2 $Parm3_2 $Parm1_2 $Parm2_2\n";
    #print "03h $Parm4_3 $Parm3_3 $Parm1_3 $Parm2_3\n";
    #print "04h $Parm4_4 $Parm3_4 $Parm1_4 $Parm2_4\n";
    #print "05h $Parm4_5 $Parm3_5 $Parm1_5 $Parm2_5\n";
    #print "06h $Parm4_6 $Parm3_6 $Parm1_6 $Parm2_6\n";
    #print "07h $Parm4_7 $Parm3_7 $Parm1_7 $Parm2_7\n";
    #print "08h $Parm4_8 $Parm3_8 $Parm1_8 $Parm2_8\n";
    #print "09h $Parm4_9 $Parm3_9 $Parm1_9 $Parm2_9\n";
    #print "10h $Parm4_10 $Parm3_10 $Parm1_10 $Parm2_10\n";

    Best regards,
    Rachid
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    Whenever I see a list of sequentially numbered vars, it tells me that the person used the wrong data structure. The most obvious data structure for this would be an array. But if I knew more about your data, I might even suggest using a hash.

    You should take a step back and redesign how you're building your data and use the proper data structure.

    For example: if you were using 4 separate arrays, the loop could look like:
    Code:
    for my $i (0..9) {
        printf("%02dh %s %s %s %s\n", $Param4[$i], $Param3[$i], $Param1[$i], $Param2[$i]);
    }
    If you were using a single AoA (Array of Arrays), the loop could look like:
    Code:
    for my $i (0..9) {
        printf("%02dh %s %s %s %s\n", $i+1, $Param[3][$i], $Param[2][$i], $Param[0][$i], $Param[1][$i]);
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    21
    Rep Power
    0
    I tested and result is nok, I got the hour and it miss the variable values:
    01h
    02h
    03h
    04h
    05h
    06h
    07h
    08h
    09h
    10h

    otherwise, I start to programm with Perl and I'm not familiarie with array.

    I tested this code, but no result:

    for($i = 1; $i < 11; $i++) {
    @liste= ("$i h",$param4_[$i],$param3_[$i],$param2__[$i],$iparam1_[$i]);
    print" @liste \n";
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    Show the code that assigns your param vars.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    21
    Rep Power
    0
    my $param3_1, $param4_1, $param2_1, $param1_1 = 0;
    my $param3_2, $param4_2, $param2_2, $param1_2 = 0;
    my $param3_3, $param4_3, $param2_3, $param1_3 = 0;
    my $param3_4, $param4_4, $param2_4, $param1_4 = 0;
    my $param3_5, $param4_5, $param2_5, $param1_5 = 0;
    my $param3_6, $param4_6, $param2_6, $param1_6 = 0;
    my $param3_7, $param4_7, $param2_7, $param1_7 = 0;
    my $param3_8, $param4_8, $param2_8, $param1_8 = 0;
    my $param3_9, $param4_9, $param2_9, $param1_9 = 0;
    my $param3_10, $param4_10, $param2_10, $param1_10 = 0;
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    Originally Posted by likeme
    my $param3_1, $param4_1, $param2_1, $param1_1 = 0;
    my $param3_2, $param4_2, $param2_2, $param1_2 = 0;
    my $param3_3, $param4_3, $param2_3, $param1_3 = 0;
    my $param3_4, $param4_4, $param2_4, $param1_4 = 0;
    my $param3_5, $param4_5, $param2_5, $param1_5 = 0;
    my $param3_6, $param4_6, $param2_6, $param1_6 = 0;
    my $param3_7, $param4_7, $param2_7, $param1_7 = 0;
    my $param3_8, $param4_8, $param2_8, $param1_8 = 0;
    my $param3_9, $param4_9, $param2_9, $param1_9 = 0;
    my $param3_10, $param4_10, $param2_10, $param1_10 = 0;
    Only the first scalar var in each of those statements will be assigned the value of 0; The other vars will not be assigned any value meaning that their value will be undef (undefined).

    There are multiple ways to assign a default value to each of them, but using vars like this is a VERY BAD APPROACH. Do not do that. Instead use 1 or more arrays.

    Your for loop is using arrays which were never defined and is why you didn't get the output you expected.

    Please post your entire script and explain what you're trying to accomplish. Include any input data that it may need and what output you expect.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    21
    Rep Power
    0
    I wrote it after 1 day of google search, I feel that he is not optimized:

    Code:
    #! /usr/bin/perl
    use feature qw/switch/;
    my $filename = $ARGV[0];
    my $fh; 
    my $current_size;
    
    open ($fh, '<', $filename) or die "Impossible to read $filename ";
    my $param41, $param31, $param21, $param11 = 0;
    my $param42, $param32, $param22, $param12 = 0;
    my $param43, $param33, $param23, $param13 = 0;
    my $param44, $param34, $param24, $param14 = 0;
    my $param45, $param35, $param25, $param15 = 0;
    my $param46, $param36, $param26, $param16 = 0;
    my $param47, $param37, $param27, $param17 = 0;
    my $param48, $param38, $param28, $param18 = 0;
    my $param49, $param39, $param29, $param19 = 0;
    my $param410, $param310, $param210, $param110 = 0;
    
    while (<$fh>) {
    # Read line from message LOG
    		my $line = $_;
    		chomp $line;
    				
    # Read file and split every line: "Feb 10 00:30:47 cd_dep01 sshd(pam_unix)[22628]: session opened for user param4 by (uid=0)"
    		my ($mois,    
    			$jour,
    			$heure,
    			$cmds,
    			$protocole,
    			$session,
    			$status, 
    			$qui,
    			$user,
    			$param,
    			$reste) = split(/ /g, $line );
    		
    	my ($h,$min,$s) = split(':', $heure);
    	
    	if (($h eq "00") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param31 = $param31 + 1; }
    						  when (/^param4/) { $param41 = $param41 + 1; }
    						  when (/^param2/) { $param21 = $param21 + 1; }
    						  when (/^param1/) { $param11 = $param11 + 1; }
    						  default       { $nothing = 1; }
    						}	
    	} 
    	if (($h eq "01") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param32 = $param32 + 1; }
    						  when (/^param4/) { $param42 = $param42 + 1; }
    						  when (/^param2/) { $param22 = $param22 + 1; }
    						  when (/^param1/) { $param12 = $param12 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 
    	if (($h eq "02") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param33 = $param33 + 1; }
    						  when (/^param4/) { $param43 = $param43 + 1; }
    						  when (/^param2/) { $param23 = $param23 + 1; }
    						  when (/^param1/) { $param13 = $param13 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 
    	if (($h eq "03") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param34 = $param34 + 1; }
    						  when (/^param4/) { $param44 = $param44 + 1; }
    						  when (/^param2/) { $param24 = $param24 + 1; }
    						  when (/^param1/) { $param14 = $param14 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 
    	if (($h eq "04") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param35 = $param35 + 1; }
    						  when (/^param4/) { $param45 = $param45 + 1; }
    						  when (/^param2/) { $param25 = $param25 + 1; }
    						  when (/^param1/) { $param15 = $param15 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 
    	
    	if (($h eq "05") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param36 = $param36 + 1; }
    						  when (/^param4/) { $param46 = $param46 + 1; }
    						  when (/^param2/) { $param26 = $param26 + 1; }
    						  when (/^param1/) { $param16 = $param16 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 
    	if (($h eq "06") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param37 = $param37 + 1; }
    						  when (/^param4/) { $param47 = $param47 + 1; }
    						  when (/^param2/) { $param27 = $param27 + 1; }
    						  when (/^param1/) { $param17 = $param17 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 
    	if (($h eq "07") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param38 = $param38 + 1; }
    						  when (/^param4/) { $param48 = $param48 + 1; }
    						  when (/^param2/) { $param28 = $param28 + 1; }
    						  when (/^param1/) { $param18 = $param18 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 	
    	if (($h eq "08") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param39 = $param39 + 1; }
    						  when (/^param4/) { $param49 = $param49 + 1; }
    						  when (/^param2/) { $param29 = $param29 + 1; }
    						  when (/^param1/) { $param19 = $param19 + 1; }
    						  default       { $nothing = 1; }
    						}
    	}
    	if (($h eq "09") and ($status eq "opened")) { 
    					
    						given ($param) {
    						  when (/^param3/) { $param310 = $param310 + 1; }
    						  when (/^param4/) { $param410 = $param410 + 1; }
    						  when (/^param2/) { $param210 = $param210 + 1; }
    						  when (/^param1/) { $param110 = $param110 + 1; }
    						  default       { $nothing = 1; }
    						}
    	} 	
    }
    
    for($i = 1; $i < 11; $i++) {
    @liste= ("$i\h",$param4[$i],$param3[$i],$param2[$i],$iparam1[$i]);
    print" @liste \n";
    }
    #print "01h 	$param31	$param41	$param11	$param21\n";
    #print "02h 	$param32	$param42	$param12	$param22\n";
    #print "03h 	$param33	$param43	$param13	$param23\n";
    #print "04h 	$param34	$param44	$param14	$param24\n";
    #print "05h 	$param35	$param45	$param15	$param25\n";
    #print "06h 	$param36	$param46	$param16	$param26\n";
    #print "07h 	$param37	$param47	$param17	$param27\n";
    #print "08h 	$param38	$param48	$param18	$param28\n";
    #print "09h 	$param39	$param49	$param19	$param29\n";
    #print "10h 	$param310	$param410	$param110	$param210\n";
    close $fh;
  14. #8
  15. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,258
    Rep Power
    1810
    It took me awhile to see what you are getting at, but I think it's something like this:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $filename = $ARGV[0];
    open my $fh, '<', $filename or die "Can't read $filename: $!";
    
    my %data;
    while (<$fh>) {
    	chomp;
    	my @field = split /\s/;
    	my ($heure, $status, $user) = @field[2,6,9];
    	my ($h,$min,$s) = split ':', $heure;
    	
    	my $hour = $h + 1;
    	$data{$user}{$hour}++;
    }
    
    # inspect the data structure by uncommenting this line
    #print Dumper \%data;
    
    # print a report from the data
    
    foreach my $user (sort keys %data) {
    	foreach my $hour (sort keys %{$data{$user}}) {
    		my $num = $data{$user}{$hour};
    		print "uid $user (hour $hour) $num tasks\n";
    	}
    	print "\n";
    }
    You are looking at only a portion of the data; just three fields really. Then grouping the number of tasks performed by user in each hour block.

    I left out a few things. It's possible to run a regular expression on the user as you did, to filter out non-matching users; but I didn't have much in the way of sample data, so I didn't.

    Also, you limited your hours to 00 - 09, and I didn't. Again, it's possible to test the number before storing and only getting the matches you want.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    21
    Rep Power
    0
    Highly optimized code (from 150 lines to 35 lines).
    Well, this is what I want as a result. I modified to get the right information.
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $filename = $ARGV[0];
    open my $fh, '<', $filename or die "Can't read $filename: $!";
    
    # Read file and split every line: "Feb 10 00:30:47 cd_dep01 sshd(pam_unix)[22628]: session opened for user param3 by (uid=0)"
    my %data;
    while (<$fh>) {
    	chomp;
    	my @field = split /\s/;
    	my ($heure, $status, $user) = @field[2,6,9];
    	my ($h,$min,$s) = split ':', $heure;
    	if ($status eq "opened"){
    		my $hour = $h + 1;
    		$data{$hour}{$user}++;
    	}
    }
    
    # inspect the data structure by uncommenting this line
    #print Dumper \%data;
    
    # print a report from the data
    
    foreach my $hour (sort keys %data) {
    	foreach my $user (sort keys %{$data{$hour}}) {
    		my $num = $data{$hour}{$user};
    		print " $hour h ($user) $num tasks\n";
    	}
    	print "\n";
    }
    Here is the result:

    1 h (param1) 23 tasks
    1 h (param2) 213 tasks
    1 h (param3) 1931 tasks

    10 h (param1) 3 tasks
    10 h (param2) 6 tasks
    10 h (param3) 5 tasks

    11 h (param2) 3 tasks
    11 h (param3) 3 tasks

    2 h (param1) 129 tasks
    2 h (param2) 4499 tasks
    2 h (param3) 3903 tasks

    3 h (param1) 1973 tasks
    3 h (param2) 3557 tasks
    3 h (param3) 3214 tasks

    4 h (param1) 2027 tasks
    4 h (param2) 3485 tasks
    4 h (param3) 3225 tasks
    ...
    Is there is some way to have following print:

    ****** param1 param2 param3
    1h 23 213 1931
    2h 129 4499 3903
    3h 1973 3557 3214
    ...
  18. #10
  19. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,258
    Rep Power
    1810
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $filename = $ARGV[0];
    open my $fh, '<', $filename or die "Can't read $filename: $!";
    
    my %data;
    my %users;
    
    while (<$fh>) {
    	chomp;
    	my @field = split /\s/;
    	my ($heure, $status, $user) = @field[2,6,9];
    	my ($h,$min,$s) = split ':', $heure;
    	
    	my $hour = $h + 1;
    	$data{$hour}{$user}++;
    	undef $users{$user};
    }
    
    # inspect the data structure by uncommenting this line
    #print Dumper \%data;
    
    # print a report from the data
    
    my @users = sort keys %users;
    print "******\t", (join "\t", @users), "\n";
    
    foreach my $hour (sort keys %data) {
    	my @row;
    	foreach my $user (@users) {
    		push @row, (exists $data{$hour}{$user} ? $data{$hour}{$user} : '0');
    	}
    	print $hour, "h\t", (join "\t", @row), "\n";
    }
    You can simplify this somewhat if you know you are wanting only specific users. I tried to write this in a way that adapts to data unknown to me.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    21
    Rep Power
    0
    This is what I'm expecting. Thanks for help.


    Result:
    Code:
    ******  param1  param2  param3
    1h      23      213     1931
    10h     3       6       5
    11h     0       3       3
    2h      129     4499    3903
    3h      1973    3557    3214
    4h      2027    3485    3225
    5h      1051    3595    3347
    6h      4       4529    4238
    7h      0       3985    3859
    8h      0       1508    1739
    9h      1       1       1

IMN logo majestic logo threadwatch logo seochat tools logo