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

<html>

Please help me to find the no of occurrences in file. also to find the max and min values. Im able to find the no of occurrences.

file =====

SQL :: select * from person ...etc , Time Taken :: 30 SQL :: select * from emp ...etc , Time Taken :: 5 SQL :: select * from home ...etc , Time Taken :: 20 SQL :: select * from emp ...etc , Time Taken :: 30 SQL :: select * from person ...etc , Time Taken :: 10 SQL :: select * from home ...etc , Time Taken :: 20 SQL :: select * from person ...etc , Time Taken :: 50
output should be =================
SQL No of occu total Min Max select * from person 3 90 10 50 select * from emp 2 35 5 30

Replies are listed 'Best First'.
Re: To find the no of occurenaces and max min value
by davido (Cardinal) on Jul 14, 2014 at 07:46 UTC

    Assuming you're able to open the input file, read it, and extract the appropriate data fields, this really is "just a matter of programming".

    One way is to keep track of max, and of min, as well as a count for max, and a count for min. Iterate over the file. Every time you find a max that is greater than your existing max, reset the counter to 1 and set your new max. Same for min. Every time you find a value that equates to your existing max, just increment the counter. Same for min. When you are done iterating, your max and your min will be known, and you will have a count for each.

    If that doesn't give you enough to go on, give use more background on what you've tried and how it's failed. A small self-contained code example that can compile and run will be useful so that we can assist in getting it to do what you want.


    Dave

Re: To find the no of occurenaces and max min value
by AppleFritter (Vicar) on Jul 14, 2014 at 09:53 UTC

    Use a hash of arrays to save the SQL statements and their running times, then use standard routines from List::Util to compute all the data you need (in other words, don't reinvent the wheel):

    #!/usr/bin/perl use feature qw/say/; use strict; use warnings; use List::Util qw/min max reduce/; my %results = (); while(<>) { chomp; my ($sql, $time) = m/^SQL :: (.*), Time Taken :: (\d+)$/; push @{ $results{$sql} }, $time; } foreach(sort keys %results) { my $occ = scalar @{ $results{$_} }; my $min = min @{ $results{$_} }; my $max = max @{ $results{$_} }; my $tot = reduce { $a + $b } 0, @{ $results{$_} }; say "$_ $occ $min $max $tot"; }

    This outputs:

    $ perl test.pl select * from emp ...etc 2 5 30 35 select * from home ...etc 2 20 20 40 select * from person ...etc 3 10 50 90 $

    P.S. I see that you crossposted your question to Stack Overflow. It is generally considered polite to inform people of this to avoid duplicated effort.

Re: To find the no of occurenaces and max min value
by vinoth.ree (Monsignor) on Jul 14, 2014 at 07:52 UTC
    use strict; use warnings; use Data::Dumper; my %hash; while(<DATA>) { chomp(); if(/from (\w+) (.*) Time Taken :: (\d+)/) { print "$1=$3\n"; $hash{$1}->{count}++; $hash{$1}->{total} +=$3; if(defined $hash{$1}->{min}) { if ($hash{$1}->{min} > $3) { $hash{$1}->{min} = $3; } } else { $hash{$1}->{min} = $3; } if(defined $hash{$1}->{max}) { if ($hash{$1}->{max} < $3) { $hash{$1}->{max} = $3; } } else { $hash{$1}->{max} = $3; } } else { print "not matching\n"; } } print Dumper \%hash; __DATA__ SQL :: select * from person ...etc , Time Taken :: 30 SQL :: select * from emp ...etc , Time Taken :: 5 SQL :: select * from home ...etc , Time Taken :: 20 SQL :: select * from emp ...etc , Time Taken :: 30 SQL :: select * from person ...etc , Time Taken :: 10 SQL :: select * from home ...etc , Time Taken :: 20 SQL :: select * from person ...etc , Time Taken :: 50

    Sorry for the lengthy code. the hash contains the details u wanted, you can format it as u want.


    All is well

      Thanks for ur reply.. I tried with above code.. but still im not able to get the min and max values correctly. only 2 hash variables Please find the code


      my %hash; open my $fh, '<', $file or die "Could not open '$file' $!"; while(<$fh>) { chomp(); if(/SQL:: (.*),Time Taken:: (.*)/i) { #print "$1=$3\n"; $hash{$1}->{count}++; $hash{$2}->{total} +=$2; if(defined $hash{$2}->{min}) { if ($hash{$2}->{min} > $2) { $hash{$2}->{min} = $2; } } else { $hash{$2}->{min} = $2; } if(defined $hash{$2}->{max}) { if ($hash{$2}->{max} < $2) { $hash{$2}->{max} = $2; } } else { $hash{$2}->{max} = $2; } } else { print "not matching\n"; } } print Dumper \%hash;
      Output should be
      ------------
      SQL No of occu Min Max select * from person 3 10 50 select * from emp 2 5 30
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: To find the no of occurenaces and max min value
by poj (Abbot) on Jul 14, 2014 at 10:42 UTC
    You could consider using a database solution
    #!perl use strict; use DBI; my $dbh = dbh('database.sqlite'); # input my $sth = $dbh->prepare('INSERT INTO DATA VALUES (?,?)'); while (<DATA>){ chomp; if ( /(select \* from \w+).*::(.*)/i ){ $sth->execute($1,$2); print "Data : [$1] [$2]\n"; } else { print "Skip : $_\n"; } } $dbh->commit; # report print '-'x55,"\n"; printf "%-20s %5s %5s %5s %5s %5s\n",('SQL','Count','Total','Min','Max +','Avg'); my $ar = $dbh->selectall_arrayref( 'SELECT F1,COUNT(F2),SUM(F2),MIN(F2),MAX(F2),AVG(F2) FROM DATA GROUP BY F1 ORDER BY F1'); for (@$ar){ printf "%-20s %5d %5d %5d %5d %5.1f\n",@$_; } close OUT; $dbh->disconnect; # create db sub dbh { my $dbfile = shift; unlink($dbfile) if (-e $dbfile); my $dbh = DBI->connect('dbi:SQLite:dbname='.$dbfile, undef, undef, {RaiseError =>1, AutoCommit =>0}) or die $DBI::errstr; $dbh->do('CREATE TABLE DATA (F1,F2 integer)'); $dbh->commit; return $dbh; } __DATA__ SQL :: select * from person ...etc , Time Taken :: 30 SQL :: select * from emp ...etc , Time Taken :: 5 SQL :: select * from home ...etc , Time Taken :: 20 SQL :: select * from emp ...etc , Time Taken :: 30 SQL :: select * from person ...etc , Time Taken :: 10 SQL :: select * from home ...etc , Time Taken :: 20 SQL :: select * from person ...etc , Time Taken :: 50
    poj
Re: To find the no of occurenaces and max min value
by Anonymous Monk on Jul 14, 2014 at 07:00 UTC
    Why sure, where is the sample data?
Re: To find the no of occurenaces and max min value
by locked_user sundialsvc4 (Abbot) on Jul 14, 2014 at 13:18 UTC

    Good grief, let’s just strip this down to SQL, because that’s all you need to do:

    SELECT
      MIN(Time_Taken) AS Minimum,
      MAX(Time_Taken) AS Maximum,
      COUNT(DISTINCT Person_ID) AS Number_of_Occur
    FROM ...
    GROUP BY person_id

    Sounds rather suspiciously like homework to me, because a trivial Pure-SQL solution does exist.