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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Format Data

 



zapzap
User

Oct 24, 2013, 10:21 AM

Post #1 of 7 (1992 views)
Format Data Can't Post

Suppose I have a data file as such
23:00 37
22:45 26
22:30 14
22:15 15
22:00 43
... more of the same ...

23:00 33
22:45 22
22:30 11
22:15 12
22:00 21
### EOF

And the goal was to create a new file where
the output was as such:

['23:00', 37, 33]
['22:45', 26, 22]
['22:30', 14, 11]
['22:15', 15, 12]
['22:00', 43, 21]

Code
open my $FH, "<", "datafile"; 

my @lines = <$FH>;
my @today = @lines[0..5];
my @yesterday = @lines[-6..-1];

my $data;

close $FH;
my $count = 0;
for(my $i = $#today; $i >= 0; $i--) {
if($today[$i] =~ /(\d+:\d+) (\d+)/) {
$data .= "['" . $1 . "'," . $2 . "," . (split(' ',$yesterday[$i]))[1] . "],";
}
}
chop $data; ## Remove extra comma ','
open $FH,">","newdatafile";
print $FH newdatafile;

Can anybody recommend a cleaner or different approach


BillKSmith
Veteran

Oct 24, 2013, 12:13 PM

Post #2 of 7 (1986 views)
Re: [zapzap] Format Data [In reply to] Can't Post

Use a hash of arrays. The hash keys would be times, and each value would be a reference to an array of integers.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Oct 24, 2013, 12:16 PM

Post #3 of 7 (1986 views)
Re: [zapzap] Format Data [In reply to] Can't Post

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):

Code
#!/usr/bin/perl 

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)


zapzap
User

Oct 24, 2013, 10:36 PM

Post #4 of 7 (1977 views)
Re: [FishMonger] Format Data [In reply to] Can't Post

Just the first and last 5 lines of code. Maybe I should have stated this explicitly? I thought the demonstration of output was good enough. I apologize.


FishMonger
Veteran / Moderator

Oct 25, 2013, 6:49 AM

Post #5 of 7 (1970 views)
Re: [zapzap] Format Data [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

chomp(my @lines = (<DATA>)[0..4,-4..-1]);

my %HoA;
foreach my $line (@lines) {
my ($key, $value) = split /\s+/, $line;
push @{ $HoA{$key} }, $value;
}

foreach my $key (sort keys %HoA) {
printf "['%s',%s]\n", $key, join(',', @{$HoA{$key}});
}


__DATA__
23:00 37
22:45 26
22:30 14
22:15 15
22:00 43
... more of the same ...
... more of the same ...
... more of the same ...
23:00 33
22:45 22
22:30 11
22:15 12
22:00 21


Output from C:\test>zapzap.pl

Quote
['22:00',43,21]
['22:15',15,12]
['22:30',14,11]
['22:45',26,22]
['23:00',37]



(This post was edited by FishMonger on Oct 25, 2013, 6:49 AM)


FishMonger
Veteran / Moderator

Oct 25, 2013, 6:52 AM

Post #6 of 7 (1968 views)
Re: [FishMonger] Format Data [In reply to] Can't Post

I had an "off by one" error.

chomp(my @lines = (<DATA>)[0..4,-4..-1]);

should be:
chomp(my @lines = (<DATA>)[0..4,-5..-1]);

And, to get the output order the way you wanted, you'll need to reverse the sort.

change:
foreach my $key (sort keys %HoA) {

to:
foreach my $key (reverse sort keys %HoA) {


(This post was edited by FishMonger on Oct 25, 2013, 6:54 AM)


zapzap
User

Oct 25, 2013, 10:39 AM

Post #7 of 7 (1958 views)
Re: [FishMonger] Format Data [In reply to] Can't Post

Thanks for your help. Looks good!

 
 


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

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