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: Need a Custom or Prewritten Perl Program?: I need a program that...:
csv parsing

 



syedarshi
New User

Sep 28, 2009, 11:10 PM

Post #1 of 2 (2514 views)
csv parsing Can't Post

Hi All,

I want to parse a big .csv file into .xls based on the keywords present on each csv line. There would be redundancy in the keywords which needs to be compiled and send to .xls file, in this way the .xls file will contain all the information with respect to each keywords in one line. I do not want to read .csv file again and again for each keyword as it takes too much of time.

for example:

.csv file format

ARL6IP2,298757,Hyperalgesia,MESH:D006930
ARL6IP2,298757,Liver Diseases,MESH:D008107
ARL6IP2,298757,"Liver Failure, Acute",MESH:D017114
ARL6IP2,298757,Liver Neoplasms,MESH:D008113
CCL22,6367,Esophageal Neoplasms,MESH:D004938
CCL22,6367,Fatty Liver,MESH:D005234
CCL22,6367,Fetal Growth Retardation,MESH:D005317
CCL22,6367,Fever,MESH:D005334

.xls file format required
() represents separate column

(ARL6IP2) (Hyperalgesia,Hyperalgesia,"Liver Failure, Acute",Liver Neoplasms) (MESH:D006930,MESH:D008107,MESH:D017114,MESH:D008113)

(CCL22) (Esophageal Neoplasms,Fatty Liver,Fetal Growth Retardation,Fever) (MESH:D004938,MESH:D005234,MESH:D005317,MESH:D005334)


Any help would be highly appreciated.

Thanks in advance

Arshi


shest75
Novice

Oct 27, 2009, 11:27 AM

Post #2 of 2 (2393 views)
Re: [syedarshi] csv parsing [In reply to] Can't Post

Bonjour,please try ( change file1.txt by the name of the file )

my %myhash;
my $ligne;
my $cpt;
my $i;

open FILE, "file1.txt" or die $!;
while (<FILE>) {
chomp($_);
@ligne=split(/,/,$_);
$cpt=split(/,/,$_);
$i=2;
while ( $i < $cpt-1 ) {
$myhash{$ligne[0]}->{desc}=$myhash{$ligne[0]}->{desc}.",".$ligne[$i]; $i++;}
$myhash{$ligne[0]}->{mesh}=$myhash{$ligne[0]}->{mesh}.",".$ligne[$cpt-1];
}

foreach my $key ( sort keys %myhash ) {
my $value = $myhash{$key};
$myhash{$key}->{desc} =~ s/,//;
$myhash{$key}->{mesh} =~ s/,//;
print "$key,$myhash{$key}->{desc},$myhash{$key}->{mesh}\n";
}

perl file.pl > result.txt to have the result in a file.


(This post was edited by shest75 on Oct 27, 2009, 11:54 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