Advertisement
dsuveges

get_table_2.pl

Dec 16th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.06 KB | None | 0 0
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. # A custom field separator can be set as a second command line argument:
  8. my $field_separator = $ARGV[1] // ";";
  9.  
  10. # The first command line argument is considered as input file:
  11. my $inputfile = $ARGV[0] // "termek.csv";
  12. open my $FH, "<", $inputfile || die "[Error] The provided file ($inputfile) could not be opened.\n";
  13.  
  14. my $data = &LoadData($FH);
  15. my $averagePrice = &GetAveragePrice($data);
  16. my $xhtml = &ListData($averagePrice);
  17. &MaxMonth($data);
  18.  
  19. #print $xhtml;
  20.  
  21. # Print out the most frequent month for all items:
  22. sub MaxMonth {
  23.     my @data = @{$_[0]};
  24.     my %maxmonth = ();
  25.    
  26.     # Month names:
  27.     my @months = qw(January February March April May Jun July August September October November December);
  28.    
  29.     # Count purchases in each mount:
  30.     foreach my $purchase (@data){
  31.         my @a = split(/\./, $purchase->{"date"});
  32.         $maxmonth{$purchase->{"item"}}{$a[1]}++;
  33.     }
  34.    
  35.     # Select most purchases for all items:
  36.     print "Item\tMax purchase\tMonth(s)\n";
  37.     foreach my $item (keys %maxmonth){
  38.         my @max_count = sort {$b cmp $a} values %{$maxmonth{$item}};
  39.         my @max_month = ();
  40.         foreach my $month (sort {$a <=> $b} keys %{$maxmonth{$item}}){
  41.             push (@max_month, $months[$month-1]) if $maxmonth{$item}{$month} == $max_count[0];
  42.         }
  43.        
  44.         print "$item\t$max_count[0]\t", join("/", @max_month), "\n";
  45.     }
  46. }
  47.  
  48. # Plot average prices:
  49. sub ListData {
  50.     my %averagePrices = %{$_[0]};
  51.     my  $tableLine = "\t<tr><td align=\"center\">%s</td><td align=\"left\">%.2f</td></tr>\n";
  52.    
  53.     my $xhtml = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"; # Initialize with doctype
  54.     $xhtml .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"hu\" lang=\"hu\">\n<head>\n\t<title>\"Mean product price.\"</title>\n</head>\n<body>"; # Add some mandatory fields and tags
  55.    
  56.     $xhtml .= "\n<table>\n\t<tr style=\"font-weight: bold;\"><td align=\"center\">Item</td><td align=\"left\">Average price</td></tr>\n"; # Opening table
  57.  
  58.     # Filling table:
  59.     foreach my $item (sort {$averagePrices{$a} <=> $averagePrices{$b}} keys %averagePrices ){
  60.         $xhtml .= sprintf($tableLine, $item, $averagePrices{$item});
  61.     }
  62.    
  63.     $xhtml .= "</table>"; # table close
  64.    
  65.     $xhtml .= "</body>\n</html>"; # closing document;
  66.     return $xhtml;
  67. }
  68.  
  69. # Get average price for all items:
  70. sub GetAveragePrice {
  71.     my @data = @{$_[0]};
  72.     my %itemPrices = (); # Contain array of prices for all items.
  73.     my %itemAveragePrices = (); # Contain the average prices for all items
  74.    
  75.     # Retrieving prices for all items:
  76.     foreach my $purchase (@data){
  77.         push( @{$itemPrices{$purchase->{"item"}}}, $purchase->{"price"});
  78.     }
  79.    
  80.     # Calculating average prices:
  81.     foreach my $item (keys %itemPrices){
  82.         $itemAveragePrices{$item} = GetAverage(@{$itemPrices{$item}});
  83.     }
  84.     return \%itemAveragePrices;
  85. }
  86.  
  87. # Helper function to get arithmetic average of a numeric array:
  88. sub GetAverage {
  89.     my $sum = 0;
  90.     foreach my $price (@_){
  91.         $sum += $price;
  92.     }
  93.     return $sum / scalar (@_);
  94. }
  95.  
  96. # Now submit the filehandle to LoadData:
  97. sub LoadData {
  98.     my $FH = $_[0];
  99.     my @array = ();
  100.     while (my $line = <$FH>){
  101.         chomp $line;
  102.         my @a = split(";", $line);
  103.         if (scalar(@a) != 4){
  104.             print STDERR "[Warning] Unexpected number of field in line: $line\n";
  105.             next;
  106.         }
  107.         push (@array, {"date" => $a[1],
  108.                        "item" => $a[2],
  109.                        "price" => $a[3]});
  110.     }
  111.     return \@array;
  112. }
  113.  
  114. __END__
  115.  
  116. A script to create sample data:
  117. perl -le '@a = qw(alma egres szolo szilva narancs retek);$s = ";";while ( $i < 200 ){$item = $a[rand @a];$price = int(rand(100));$date = sprintf("%s.%s.%s %s:%s",int(rand(30) + 1990),int(rand(12)+1),int(rand(31)+1),int(rand(24)+1),int(rand(60)+1));$i++;print join $s, $i, $date,$item, $price}' > sample.data.csv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement