#!/usr/bin/perl -w

use strict;			# Mandatory

use URI;			# query(), query_form();
use URI::Escape;

# --- ignore everything from here...
my %data;

# Pull the data in
open(DATA, 'output3') or die "open(): $!\n";
{
  local $/;
  my $code = <DATA>;
  my $var1 = eval $code;
  if($@) {
    die "eval: $@\n";
  }

  %data = %$var1;
}
close(DATA);
# --- to here

# Pass 1.  Work through the data, extract a list of all the possible column
# names we might use.  Store them in %columns while we go, then build the
# full list in to @COLUMNS.

my @COLUMNS = ();		# Final list of columns, in order
my %columns = ();		# Columns known so far

$columns{'hash_key'}++;		# 'hash_key' is a known column name

my $uri = URI->new();		# Use URI methods to split query strings

foreach my $val (values %data) {
  # $val is application/x-www-form-urlencoded key/value pairs.  Break it out
  # in to seperate keys and values.  Each key will become a column name

  my %ukv;			# Will hold URI key/values
  $uri->query($val);		# Initialise the URI query
  %ukv = $uri->query_form;	# Split the line

  # Each key in %ukv will be a column name
  foreach (keys %ukv) {		# $_ is the key value
    $columns{$_}++;
  }
}

@COLUMNS = sort keys %columns;	# Build the final list of columns
#print "Found the following distinct column names\n";
#print join("\n", @COLUMNS), "\n";

# Print the column headers
print join(',', @COLUMNS), "\n";

# Pass 2.  Work through the hash, one entry at a time.  Split each entry out
# in to its own hash, and then format the results as DSV and display.

foreach my $key (keys %data) {
  my %ukv;			# Will hold URI key/values
  $uri->query($data{$key});	# Initialise the URI query
  %ukv = $uri->query_form;	# Split the line
  $ukv{'hash_key'} = $key;

  # Iterate over the values in %ukv, pushing the values in to @output in the
  # order in which they will be displayed.
  #
  # URI escaped values are unescaped, and any commas in the output have a
  # backslash inserted to escape them.

  my @output = ();
  foreach my $column (@COLUMNS) {
    if(! exists $ukv{$column} or !defined $ukv{$column}) {
      push @output, '';
      next;
    }

    $ukv{$column} = uri_unescape($ukv{$column}); # URI unescaping
    $ukv{$column} =~ s/(?<=\\),/\\,/; # Escape unescaped commas

    push @output, $ukv{$column};
  }

  # Join @output with commas, and print
  print join(',', @output), "\n";
}
