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: Advanced:
how do i keep track repeated keys with different values.

 



subodheee
Novice

Dec 27, 2009, 9:18 PM

Post #1 of 2 (1686 views)
how do i keep track repeated keys with different values. Can't Post

hi,

i have a csv file with repeated keys with different values, i need to add values for the repeated keys. i tried using hashes but it takes the only last value. is there a way that i can keep track of repeated keys and add values for each repeated key.

example: am taking key as a time. same time 40:09.1 repeats for 4 times. for this 4 times i want to add correponding values. when i used hash this is not happening.

time value1 value2 value3
40:09.1 32.3 33.4 21.3
40:09.1 21.3 35.4 22.6
40:09.1 22.4 19.2 14.9
41:01.1 12.3 11.2 12.3
40:09.1 31.2 11.2 19.3

can anyone help me here.


(This post was edited by subodheee on Dec 27, 2009, 9:21 PM)


shawnhcorey
Enthusiast


Dec 28, 2009, 6:57 AM

Post #2 of 2 (1674 views)
Re: [subodheee] how do i keep track repeated keys with different values. [In reply to] Can't Post

You would use a complex data structure. For more details, see `perldoc perldsc` or http://perldoc.perl.org/perldsc.html


Code
#!/usr/bin/perl 

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;

my %data = ();
while( <DATA> ){
chomp;
my ( $time, @value ) = split;
push @{ $data{$time} }, \@value;
}

print Dumper \%data;

__DATA__
40:09.1 32.3 33.4 21.3
40:09.1 21.3 35.4 22.6
40:09.1 22.4 19.2 14.9
41:01.1 12.3 11.2 12.3
40:09.1 31.2 11.2 19.3


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


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

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