Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/perl
- use strict;
- use warnings;
- use Data::Dumper;
- # A custom field separator can be set as a second command line argument:
- my $field_separator = $ARGV[1] // ";";
- # The first command line argument is considered as input file:
- my $inputfile = $ARGV[0] // "termek.csv";
- open my $FH, "<", $inputfile || die "[Error] The provided file ($inputfile) could not be opened.\n";
- my $data = &LoadData($FH);
- my $averagePrice = &GetAveragePrice($data);
- my $xhtml = &ListData($averagePrice);
- &MaxMonth($data);
- #print $xhtml;
- # Print out the most frequent month for all items:
- sub MaxMonth {
- my @data = @{$_[0]};
- my %maxmonth = ();
- # Month names:
- my @months = qw(January February March April May Jun July August September October November December);
- # Count purchases in each mount:
- foreach my $purchase (@data){
- my @a = split(/\./, $purchase->{"date"});
- $maxmonth{$purchase->{"item"}}{$a[1]}++;
- }
- # Select most purchases for all items:
- print "Item\tMax purchase\tMonth(s)\n";
- foreach my $item (keys %maxmonth){
- my @max_count = sort {$b cmp $a} values %{$maxmonth{$item}};
- my @max_month = ();
- foreach my $month (sort {$a <=> $b} keys %{$maxmonth{$item}}){
- push (@max_month, $months[$month-1]) if $maxmonth{$item}{$month} == $max_count[0];
- }
- print "$item\t$max_count[0]\t", join("/", @max_month), "\n";
- }
- }
- # Plot average prices:
- sub ListData {
- my %averagePrices = %{$_[0]};
- my $tableLine = "\t<tr><td align=\"center\">%s</td><td align=\"left\">%.2f</td></tr>\n";
- 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
- $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
- $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
- # Filling table:
- foreach my $item (sort {$averagePrices{$a} <=> $averagePrices{$b}} keys %averagePrices ){
- $xhtml .= sprintf($tableLine, $item, $averagePrices{$item});
- }
- $xhtml .= "</table>"; # table close
- $xhtml .= "</body>\n</html>"; # closing document;
- return $xhtml;
- }
- # Get average price for all items:
- sub GetAveragePrice {
- my @data = @{$_[0]};
- my %itemPrices = (); # Contain array of prices for all items.
- my %itemAveragePrices = (); # Contain the average prices for all items
- # Retrieving prices for all items:
- foreach my $purchase (@data){
- push( @{$itemPrices{$purchase->{"item"}}}, $purchase->{"price"});
- }
- # Calculating average prices:
- foreach my $item (keys %itemPrices){
- $itemAveragePrices{$item} = GetAverage(@{$itemPrices{$item}});
- }
- return \%itemAveragePrices;
- }
- # Helper function to get arithmetic average of a numeric array:
- sub GetAverage {
- my $sum = 0;
- foreach my $price (@_){
- $sum += $price;
- }
- return $sum / scalar (@_);
- }
- # Now submit the filehandle to LoadData:
- sub LoadData {
- my $FH = $_[0];
- my @array = ();
- while (my $line = <$FH>){
- chomp $line;
- my @a = split(";", $line);
- if (scalar(@a) != 4){
- print STDERR "[Warning] Unexpected number of field in line: $line\n";
- next;
- }
- push (@array, {"date" => $a[1],
- "item" => $a[2],
- "price" => $a[3]});
- }
- return \@array;
- }
- __END__
- A script to create sample data:
- 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