Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
No such thing as a small change
 
PerlMonks  

Need help in file processing

by utpalmtbi (Acolyte)
on Feb 01, 2013 at 07:48 UTC ( #1016472=perlquestion: print w/ replies, xml ) Need Help??
utpalmtbi has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I just started learning Perl and I get stuck on a basic file processing :

I have to convert a file format, the program should read a input file and after processing (which counts the number of each term in each group, see below), yield a output file. Here is the format of the input file:

Group1: somo|112345478 somo|734567233 homo|233689876

Group2: somo|904686712 somo|891145662 somo|106736432

Group3: aomo|397634567 aomo|123446789

Group4: aomo|905672345 aomo|120846789

........

..

From the above input, the output file should look like (the numbers after the pipe should not be considered):

somo homo aomo

Group1 2 1 0

Group2 3 0 0

Group3 0 0 2

Group4 0 0 2

I get stuck on this file conversion for sometime now and unable to figure it out. Any guidance is highly appreciated. Thanks in advance.. Apologies if this post doesn't belong here...

Comment on Need help in file processing
Re: Need help in file processing
by Anonymous Monk on Feb 01, 2013 at 08:10 UTC
Re: Need help in file processing
by vinoth.ree (Vicar) on Feb 01, 2013 at 09:30 UTC
    use strict; use warnings; use Data::Dumper; my %hash; my @names; while(<DATA>) { my ($Groupname,@Data) = split(/\s+/,$_); foreach(@Data) { my ($name,$count) = split(/\|/,$_); unless ( grep( /^$name$/, @names ) ) { push(@names,$name); } $hash{$Groupname}->{$name}++; } } &Format(\%hash,\@names); sub Format { my ($hash_ref,$name_ref) = @_; my @Names = sort @$name_ref; my @Group_names = keys %{$hash_ref}; my $filename = "test.txt"; #Give File Path open my $fh, '>>', $filename or die "Can't write to '$filename +': $!\n"; print $fh "\t@Names\n"; foreach my $g_name (@Group_names) { print $fh "$g_name\t"; foreach my $name(@Names) { unless (exists $hash_ref->{$g_name}->{$name}) { print $fh qq{0\t}; } else { print $fh $hash_ref->{$g_name}->{$name}.qq{\t}; } } print OUT qq{\n}; } } __DATA__ Group1: somo|112345478 somo|734567233 homo|233689876 Group2: somo|904686712 somo|891145662 somo|106736432 Group3: aomo|397634567 aomo|123446789 Group4: aomo|905672345 aomo|120846789

    Use open() function to open the input file and read each line using while loop, instead of using <DATA>

    Update:

    Updated the file open() safer method, as per Arunbear advise! Thanks Arunbear ++

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016472]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2014-04-24 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls