CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Re: [zapzap] Format Data: Edit Log

Veteran / Moderator

Oct 24, 2013, 12:16 PM

Views: 5644
Re: [zapzap] Format Data

Do you need to process the entire file or just the first and last 5 lines like your posted code?

I'd load the data into a HoA (Hash of Arrays) where the first column is the key and push the second column onto the hash.

Try this (untested):


use strict;
use warnings;

my %HoA; # use a var name that describes the data that is being held

open my $input_fh, '<', 'datafile' or die "failed to open 'datafile' $!";
while (my $line = <$input_fh>) {
chomp $line;
my ($key, $value) = split /\s+/, $line;
push @{ $HoA{$key} }, $value;
close $input_fh;

open my $output_fh, '>', 'newdatafile' or die "failed to open 'newdatafile' $!";
foreach my $key (sort keys %HoA) {
printf "['%s',%s]\n", $key, join(',', @{$HoA{$key}});
close $output_fh;

(This post was edited by FishMonger on Oct 24, 2013, 12:17 PM)

Edit Log:
Post edited by FishMonger (Veteran) on Oct 24, 2013, 12:17 PM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives