Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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] ? $ARGV[1] : ";";
- # The first command line argument is considered as input file:
- my $inputfile = $ARGV[0] ? $ARGV[0] : "termek.csv";
- my %hash = ();
- open ( my $INPUT, "<", $inputfile) || die "[Error] Input file ($inputfile) could not be opened.\n";
- while ( my $line = <$INPUT>){
- # Input line example:
- # 1;2004. 01.17 13:19;Eper;5997.35
- chomp $line;
- my ($index, $date, $item, $price) = split /$field_separator/, $line;
- next unless defined $price and defined $item; # we skip all those lines where these fields are not given.
- $hash{$item}{purchase_cnt} += 1;
- $hash{$item}{price} += $price;
- }
- # Let's see how the read data looks like:
- # print Dumper %hash; # OK, it seems the file is read as expected!
- # Now print the data:
- my $html = "<!DOCTYPE HTML>\n<html>\n<head><title>Summary table</title>\n</head>\n<body>"; # html header:
- $html .= "<table style=\"text-align:center;\">\n\t<tr style=\"font-weight: bold;\"><td>Item</td><td>Times purchased</td><td>Total price</td></tr>\n"; # table header
- # Looping through all items:
- foreach my $item (keys %hash){
- $html .= sprintf "\t<tr><td style=\"font-weight: bold;\">%s</td><td>%s</td><td>%s</td></tr>\n", $item, $hash{$item}{purchase_cnt}, $hash{$item}{price};
- }
- $html .= "</table>"; # table close
- $html .= "\n</body>\n</html>"; # html close
- print $html;
- __END__
- the below one-liner was used to generate an input of 200 lines:
- perl -le '@a = qw(alma egres szolo szilva); $date = "2016.10.13 20:45"; while ( $i < 200 ){ $item = $a[rand @a]; $price = int(rand(100));$i++;print join ";", $i, $date,$item, $price}' > sample.data.csv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement