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: Beginner:
Build a hash from a text file with 1 key and 2 values [SOLVED]

 



schnubbex
Novice

Aug 26, 2014, 5:04 PM

Post #1 of 9 (1485 views)
Build a hash from a text file with 1 key and 2 values [SOLVED] Can't Post

Hello Forum,
I have a problem with filling a hash from a txt file.
The file goes something like this

key1 value1 value2
key2 value1 value2

and has like 30 rows
so my code is the following:

Code
#!/usr/bin/perl 
use strict;
use warnings;


open ( my $peak_fh, "<", "input1.txt");
my %Data2Hash;


while ( my $line = <$peak_fh> ) {
chomp $line;
next if $line =~ /1337/; #Skip Header
my ( $data ) = (split( '\t \s', $line ))[0];
push @{$Data2Hash{$data}},$line ;
}
print "Key: $_ and Value: $Data2Hash{$_}\n" foreach (keys%Data2Hash);
close $peak_fh;

i get the entire row as the key but i want only the first entry in a row as the key and the following 2 entrys as the values

the txt file is not formated so sometimes there are tabs and sometimes whitespaces or both between each entry
Any idias?


(This post was edited by schnubbex on Aug 26, 2014, 6:29 PM)


FishMonger
Veteran / Moderator

Aug 26, 2014, 5:39 PM

Post #2 of 9 (1477 views)
Re: [schnubbex] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

Please post some sample lines from your input file.

The first arg to split should be a regex pattern, not a string and you should split it into the 3 needed values.

The first will be the key and the push statement will push an array ref of the other 2 values .


Zhris
Enthusiast

Aug 26, 2014, 5:47 PM

Post #3 of 9 (1475 views)
Re: [schnubbex] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

Hi,


Quote
the txt file is not formated so sometimes there are tabs and sometimes whitespaces or both between each entry


When you split the row, use a regular expression to split on one or more of any type of whitespace characters.


Quote
i get the entire row as the key but i want only the first entry in a row as the key and the following 2 entrys as the values


Assign the key to a scalar and the rest of the values to an array on the left side of the split.

While considering the above two points, your split and push would look something like:


Code
my ($key, @values) = split /\s+/, $line; 
push @{$Data2Hash{$key}}, @values; # or possibly $Data2Hash{$key} = \@values to overwrite if duplicate keys.


In order to print the list of values correctly, you'll also need to de-reference the array:


Code
# $" = ', '; 
print "Key: $_ and Value(s): @{$Data2Hash{$_}}\n" foreach (keys%Data2Hash);


Regards,

Chris


(This post was edited by Zhris on Aug 26, 2014, 5:52 PM)


schnubbex
Novice

Aug 26, 2014, 5:54 PM

Post #4 of 9 (1472 views)
Re: [FishMonger] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

The original file is much larger so i made a copy that is smaller and easier to understand


(This post was edited by schnubbex on Aug 26, 2014, 5:57 PM)
Attachments: input1.txt (90 B)


schnubbex
Novice

Aug 26, 2014, 6:06 PM

Post #5 of 9 (1464 views)
Re: [Zhris] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

Hi,
I tried your solution but my output is still the same as before:

Key: Q and Value(s): ARRAY(0x376028)
Key: AO and Value(s): ARRAY(0x36c0a0)
Key: U and Value(s): ARRAY(0x375ea8)
Key: O and Value(s): ARRAY(0x3760e8)
Key: N and Value(s): ARRAY(0x376148)
Key: J and Value(s): ARRAY(0x3762c8)
Key: AE and Value(s): ARRAY(0x36c4f0)
Key: AP and Value(s): ARRAY(0x36c040)
Key: AN and Value(s): ARRAY(0x36c100)
Key: AF and Value(s): ARRAY(0x36c490)
Key: T and Value(s): ARRAY(0x375f08)
Key: AA and Value(s): ARRAY(0x36c670)
Key: M and Value(s): ARRAY(0x3761a8)
Key: AB and Value(s): ARRAY(0x36c610)
Key: Y and Value(s): ARRAY(0x36c730)
Key: W and Value(s): ARRAY(0x36c7f0)
Key: G and Value(s): ARRAY(0x3763e8)
Key: C and Value(s): ARRAY(0x344ce8)
Key: R and Value(s): ARRAY(0x375fc8)
Key: X and Value(s): ARRAY(0x36c790)
Key: V and Value(s): ARRAY(0x375e48)
Key: AK and Value(s): ARRAY(0x36c220)
Key: S and Value(s): ARRAY(0x375f68)
Key: AC and Value(s): ARRAY(0x36c5b0)
Key: Z and Value(s): ARRAY(0x36c6d0)
Key: AG and Value(s): ARRAY(0x36c430)
Key: D and Value(s): ARRAY(0x344e38)
Key: I and Value(s): ARRAY(0x376328)
Key: AD and Value(s): ARRAY(0x36c550)
Key: H and Value(s): ARRAY(0x376388)
Key: B and Value(s): ARRAY(0x2dc158)
Key: P and Value(s): ARRAY(0x376088)
Key: A and Value(s): ARRAY(0x34c7a8)
Key: AH and Value(s): ARRAY(0x36c340)
Key: L and Value(s): ARRAY(0x376208)
Key: AL and Value(s): ARRAY(0x36c1c0)
Key: AI and Value(s): ARRAY(0x36c2e0)
Key: AJ and Value(s): ARRAY(0x36c280)
Key: E and Value(s): ARRAY(0x376568)
Key: AM and Value(s): ARRAY(0x36c160)
Key: K and Value(s): ARRAY(0x376268)
Key: F and Value(s): ARRAY(0x376448)

but i want to have the same as in the file (with the actual value) and not this ARRAY(0x376448) for example


Zhris
Enthusiast

Aug 26, 2014, 6:17 PM

Post #6 of 9 (1457 views)
Re: [schnubbex] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

Please post your updated code.

Chris


FishMonger
Veteran / Moderator

Aug 26, 2014, 6:25 PM

Post #7 of 9 (1454 views)
Re: [schnubbex] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

This is based on your posted example source file.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $file = 'input1.txt';

open my $peak_fh, "<", $file
or die "failed to open '$file' $!";

my %Data2Hash;

<$peak_fh>; # skip header
while ( my $line = <$peak_fh> ) {
chomp $line;
next if $line =~ /^\s*$/;
my ($key, @values) = split /\s+/, $line;
$Data2Hash{$key} = \@values;
}
close $peak_fh;

printf "%-4s %7s %7s\n", 'Key', 'value1', 'value2';
print '=' x 20 . "\n";
foreach my $key (sort keys %Data2Hash) {
printf "%-4s %7d %7d\n", $key, @{$Data2Hash{$key}};
}


outputs:

Code
Key   value1  value2 
====================
A 2 4
B 65 74
C 109 120
D 33 39
E 10 14
F 2 4
G 3 5



(This post was edited by FishMonger on Aug 26, 2014, 6:30 PM)


schnubbex
Novice

Aug 26, 2014, 6:27 PM

Post #8 of 9 (1449 views)
Re: [Zhris] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

lol, now it works somehow.
Maybe i typed something wrong.
I thank you so mutch for this


Zhris
Enthusiast

Aug 26, 2014, 6:53 PM

Post #9 of 9 (1438 views)
Re: [schnubbex] Build a hash from a text file with 1 key and 2 values [In reply to] Can't Post

No problem. Make sure you look at / use Fishmonger's improved version of your code above, as oppose to just making the changes we outlined, they were simply quick-fix suggestions.

Chris

 
 


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

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