R
rickle
I'm looking for some assistance from the perl folk out there. I am a
perl hack who writes a few scripts a year in perl only when needed.
This seems like a job best suited for perl (most likely a hash) but I
am fumbling around more then I would like to be. I'm sure this is
very simple for anyone well versed in perl.
Objective:
Take a list of named values and put them into a CSV file. When there
isn't a named value there should just be an empty CSV slot. There
might also be some entries on the same line that are somewhat
duplicated, where if there is one entry it should always trump the
other. The CSV file will always have 7 possible entries in the CSV.
language,format,country,zip,category,ua,id
Problem:
The named values vary by line so there is never just X per line. Some
will have just X, some will be X+1, X+5, some will be empty, etc.,
etc.
Example file:
l=en&format=xhtml
format=xml&country=US&ua=Mozilla
l=sp&zip=00000&category=books
l=en&format=xml&id=xyz
l=fr&country=US&alt-country=CA # in this case we want the alt-
country to populate the country field
Example output:
en,xhtml,,,,,,
,xml,US,,,Mozilla,
sp,,,00000,books,,
en,xml,,,,,xyz
fr,,CA,,,,,
I have tried playing around with the URI perl module but haven't had
much luck. I have also made some attempts on my own but I am just not
getting things right. I know this is probably better suited to a hash
but I am very hash illiterate. I can perform basic functions in
hashes and do simple stuff but I don't play around with perl enough to
have gotten any better.
foreach (@DATA_SET) {
next unless /\S/; # strip out blank lines, i.e. no named
values
# print "$_\n";
#$format = m/format=xml/;
#my $format =~ /format=xml/;
#print $_;
#print "$format\n";
}
I also tried pushing the results to another function and doing some
work there but it didn't go well as you can see from how I ended up
completely commenting it out.
#sub string_analysis {
#my (@DATA_RESULTS) = @_;
#@DATA_RESULTS = split(/&/,$_ [0]);
#print "@DATA_RESULTS\n";
#while(@DATA_RESULTS){
# foreach (split/&/,$S)[0]){
# print "$DATA_RESULTS[1]\n";
# }
#}
# push (@SPLIT_DATA_RESULTS = split(/\&/,$_));
#}
#while (@SPLIT_DATA_RESULTS) {
# print "$_\n";
#}
#my @DATA_STRING = split /&/, @fields[9];
#print "@DATA_STRING[1], @DATA_STRING[2], @DATA_STRING[3],
@DATA_STRING[4], @DATA_STRING[5], @DATA_STRING[6], @DATA_STRING[7],
@DATA_STRING[8]\n";
#}
Any help would be greatly appreciated.
perl hack who writes a few scripts a year in perl only when needed.
This seems like a job best suited for perl (most likely a hash) but I
am fumbling around more then I would like to be. I'm sure this is
very simple for anyone well versed in perl.
Objective:
Take a list of named values and put them into a CSV file. When there
isn't a named value there should just be an empty CSV slot. There
might also be some entries on the same line that are somewhat
duplicated, where if there is one entry it should always trump the
other. The CSV file will always have 7 possible entries in the CSV.
language,format,country,zip,category,ua,id
Problem:
The named values vary by line so there is never just X per line. Some
will have just X, some will be X+1, X+5, some will be empty, etc.,
etc.
Example file:
l=en&format=xhtml
format=xml&country=US&ua=Mozilla
l=sp&zip=00000&category=books
l=en&format=xml&id=xyz
l=fr&country=US&alt-country=CA # in this case we want the alt-
country to populate the country field
Example output:
en,xhtml,,,,,,
,xml,US,,,Mozilla,
sp,,,00000,books,,
en,xml,,,,,xyz
fr,,CA,,,,,
I have tried playing around with the URI perl module but haven't had
much luck. I have also made some attempts on my own but I am just not
getting things right. I know this is probably better suited to a hash
but I am very hash illiterate. I can perform basic functions in
hashes and do simple stuff but I don't play around with perl enough to
have gotten any better.
foreach (@DATA_SET) {
next unless /\S/; # strip out blank lines, i.e. no named
values
# print "$_\n";
#$format = m/format=xml/;
#my $format =~ /format=xml/;
#print $_;
#print "$format\n";
}
I also tried pushing the results to another function and doing some
work there but it didn't go well as you can see from how I ended up
completely commenting it out.
#sub string_analysis {
#my (@DATA_RESULTS) = @_;
#@DATA_RESULTS = split(/&/,$_ [0]);
#print "@DATA_RESULTS\n";
#while(@DATA_RESULTS){
# foreach (split/&/,$S)[0]){
# print "$DATA_RESULTS[1]\n";
# }
#}
# push (@SPLIT_DATA_RESULTS = split(/\&/,$_));
#}
#while (@SPLIT_DATA_RESULTS) {
# print "$_\n";
#}
#my @DATA_STRING = split /&/, @fields[9];
#print "@DATA_STRING[1], @DATA_STRING[2], @DATA_STRING[3],
@DATA_STRING[4], @DATA_STRING[5], @DATA_STRING[6], @DATA_STRING[7],
@DATA_STRING[8]\n";
#}
Any help would be greatly appreciated.