Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Need help in file processing

by vinoth.ree (Monsignor)
on Feb 01, 2013 at 09:30 UTC ( #1016485=note: print w/replies, xml ) Need Help??

in reply to Need help in file processing

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>


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

Replies are listed 'Best First'.
Re^2: Need help in file processing
by Arunbear (Parson) on Feb 01, 2013 at 11:09 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016485]
[tye]: Is it just me, or do most python things have terrible logging/errors?
[LanX]: but but but ... the errors are nicely formatted!
[tye]: 1) The python stack traces show you the names of variables, not the values of variables. Almost never informative.
[LanX]: do you have a py job now?
[tye]: Right now I have errors trying to fetch stuff from graphite. It says 'apps not ready, check startup logs'. I eventually find the startup logs and see 'no apps found'. Looking for fixes to this problem...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2017-09-21 20:03 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (252 votes). Check out past polls.