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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [cluna] Syntax Errors In Program With Hash: Edit Log


Mar 17, 2013, 10:02 AM

Views: 4487
Re: [cluna] Syntax Errors In Program With Hash

The situation you're describing calls for a hash of arrays (HoA), where the key is the region and its associated value is a reference to a list of p-values. Consider the following option:

use strict; 
use warnings;

my ( $numFiles, %hash );

for my $file (<*.txt>) {
open my $fh, '<', $file or die $!;

while (<$fh>) {
my ( $region, $pValue ) = (split)[ 1, 4 ];
push @{ $hash{$region} }, $pValue;
close $fh;

for my $region ( sort keys %hash ) {
if ( @{ $hash{$region} } == $numFiles ) {
print "$region\t" . ( join "\t", @{ $hash{$region} } ) . "\n";

As you have, a file glob is used to get a listing of the current dir's *.txt files. Each file is processed using the three-argument form of open. Each file line (in Perl's default scalar) is split and sliced to grab only the region and p-value entries. Then the p-value is pushed onto the list that's keyed by region. A variable that counts the number of files is finally incremented.

Using your region/p-value dataset example, the hash structure would look like the following:

$VAR1 = { 
'np1000056' => [

Thus, each region, as key, would be associated with a list of p-values. The next step is to (selectively) print these, and that's what occurs within the last for.

The last segment sorts the keys, and then evaluates whether the number of items in the list associated to the region via a reference is equal to the number of files (i.e., whether there is an associated p-value for each file). If so, it prints the key and those values as a tab-delimited line.

To save the output to a file, you can do the following from the command line:

perl >resultsFile

The second argument just directs output to a file. Without it, the results are printed to the screen.

This has been tested, but let me know if you encounter any issues or have any questions.

Hope this helps!

PS - My compliments on your clearly-articulated specs and problem, and your use of strict and warnings at the top of your script.

(This post was edited by Kenosis on Mar 17, 2013, 10:19 AM)

Edit Log:
Post edited by Kenosis (User) on Mar 17, 2013, 10:06 AM
Post edited by Kenosis (User) on Mar 17, 2013, 10:10 AM
Post edited by Kenosis (User) on Mar 17, 2013, 10:14 AM
Post edited by Kenosis (User) on Mar 17, 2013, 10:19 AM

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

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