Creating multiple hashes from multiple files in one go
up vote
0
down vote
favorite
I want to perform a vlookup like process but with multiple files wherein the contents of the first column from all files (sorted n uniq-ed) is reference value. Now I would like to store these key-values pairs from each file in each hash and then print them together. Something like this:
file1: while()$hash1$key=$val...file2: while()$hash2$key=$val...file3: while()$hash3$key=$val...so on
Then print it: print "$ref_val $hash1$ref_val $hash3$ref_val $hash3$ref_val..."
$i=1;
@FILES = @ARGV;
foreach $file(@FILES)
open($fh,$file);
$hname="hash".$i; ##trying to create unique hash by attaching a running number to hash name
while(<$fh>)@d=split("t");$hname$d[0]=$d[7];$i++;
$set=$i-1; ##store this number for recreating the hash names during printing
open(FH,"ref_list.txt");
while(<FH>)
chomp();print "$_t";
## here i run the loop recreating the hash names and printing its corresponding value
for($i=1;$i<=$set;$i++)$hname="hash".$i; print "$hname$_t";
print "n";
Now this where I am stuck perl takes $hname as hash name instead of $hash1, $hash2...
Thanks in advance for the helps and opinions
perl hash
|
show 2 more comments
up vote
0
down vote
favorite
I want to perform a vlookup like process but with multiple files wherein the contents of the first column from all files (sorted n uniq-ed) is reference value. Now I would like to store these key-values pairs from each file in each hash and then print them together. Something like this:
file1: while()$hash1$key=$val...file2: while()$hash2$key=$val...file3: while()$hash3$key=$val...so on
Then print it: print "$ref_val $hash1$ref_val $hash3$ref_val $hash3$ref_val..."
$i=1;
@FILES = @ARGV;
foreach $file(@FILES)
open($fh,$file);
$hname="hash".$i; ##trying to create unique hash by attaching a running number to hash name
while(<$fh>)@d=split("t");$hname$d[0]=$d[7];$i++;
$set=$i-1; ##store this number for recreating the hash names during printing
open(FH,"ref_list.txt");
while(<FH>)
chomp();print "$_t";
## here i run the loop recreating the hash names and printing its corresponding value
for($i=1;$i<=$set;$i++)$hname="hash".$i; print "$hname$_t";
print "n";
Now this where I am stuck perl takes $hname as hash name instead of $hash1, $hash2...
Thanks in advance for the helps and opinions
perl hash
You are trying to use symbolic reference, to create variable names dynamically; it's only trouble and (almost) never necessary. Why not simply put references to those hashes in an array?
– zdim
yesterday
The hash is made with key of the first element ($d[0]
) and value of ... the eight-th ($d[7]
) -- is that intended?
– zdim
yesterday
@zdim...yes, correctly understood. Each line has 10 columns, but i need to form the hash out of column 1 (key) and column 7 (value)....yes that symbolic reference aspect did come, but i wasn't sure how to apply it here... :(
– SangramJB
yesterday
OK, thank you. The symbolic reference business -- you don't want it. Just don't do it. Now, your explanation says that "contents of the first column from all files (sorted n uniq-ed) is reference value" -- how is that? The "first column" from all files is a long list of values, how is that a reference? Can you show a sample?
– zdim
yesterday
OK...now i have files like : exp1.txt, exp2.txt, exp3.txt so on. To collect and create a list of all reference values I do this :cut -f 1 *.txt | sort | uniq > ref_list.txt
– SangramJB
yesterday
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to perform a vlookup like process but with multiple files wherein the contents of the first column from all files (sorted n uniq-ed) is reference value. Now I would like to store these key-values pairs from each file in each hash and then print them together. Something like this:
file1: while()$hash1$key=$val...file2: while()$hash2$key=$val...file3: while()$hash3$key=$val...so on
Then print it: print "$ref_val $hash1$ref_val $hash3$ref_val $hash3$ref_val..."
$i=1;
@FILES = @ARGV;
foreach $file(@FILES)
open($fh,$file);
$hname="hash".$i; ##trying to create unique hash by attaching a running number to hash name
while(<$fh>)@d=split("t");$hname$d[0]=$d[7];$i++;
$set=$i-1; ##store this number for recreating the hash names during printing
open(FH,"ref_list.txt");
while(<FH>)
chomp();print "$_t";
## here i run the loop recreating the hash names and printing its corresponding value
for($i=1;$i<=$set;$i++)$hname="hash".$i; print "$hname$_t";
print "n";
Now this where I am stuck perl takes $hname as hash name instead of $hash1, $hash2...
Thanks in advance for the helps and opinions
perl hash
I want to perform a vlookup like process but with multiple files wherein the contents of the first column from all files (sorted n uniq-ed) is reference value. Now I would like to store these key-values pairs from each file in each hash and then print them together. Something like this:
file1: while()$hash1$key=$val...file2: while()$hash2$key=$val...file3: while()$hash3$key=$val...so on
Then print it: print "$ref_val $hash1$ref_val $hash3$ref_val $hash3$ref_val..."
$i=1;
@FILES = @ARGV;
foreach $file(@FILES)
open($fh,$file);
$hname="hash".$i; ##trying to create unique hash by attaching a running number to hash name
while(<$fh>)@d=split("t");$hname$d[0]=$d[7];$i++;
$set=$i-1; ##store this number for recreating the hash names during printing
open(FH,"ref_list.txt");
while(<FH>)
chomp();print "$_t";
## here i run the loop recreating the hash names and printing its corresponding value
for($i=1;$i<=$set;$i++)$hname="hash".$i; print "$hname$_t";
print "n";
Now this where I am stuck perl takes $hname as hash name instead of $hash1, $hash2...
Thanks in advance for the helps and opinions
perl hash
perl hash
edited yesterday
asked yesterday
SangramJB
105
105
You are trying to use symbolic reference, to create variable names dynamically; it's only trouble and (almost) never necessary. Why not simply put references to those hashes in an array?
– zdim
yesterday
The hash is made with key of the first element ($d[0]
) and value of ... the eight-th ($d[7]
) -- is that intended?
– zdim
yesterday
@zdim...yes, correctly understood. Each line has 10 columns, but i need to form the hash out of column 1 (key) and column 7 (value)....yes that symbolic reference aspect did come, but i wasn't sure how to apply it here... :(
– SangramJB
yesterday
OK, thank you. The symbolic reference business -- you don't want it. Just don't do it. Now, your explanation says that "contents of the first column from all files (sorted n uniq-ed) is reference value" -- how is that? The "first column" from all files is a long list of values, how is that a reference? Can you show a sample?
– zdim
yesterday
OK...now i have files like : exp1.txt, exp2.txt, exp3.txt so on. To collect and create a list of all reference values I do this :cut -f 1 *.txt | sort | uniq > ref_list.txt
– SangramJB
yesterday
|
show 2 more comments
You are trying to use symbolic reference, to create variable names dynamically; it's only trouble and (almost) never necessary. Why not simply put references to those hashes in an array?
– zdim
yesterday
The hash is made with key of the first element ($d[0]
) and value of ... the eight-th ($d[7]
) -- is that intended?
– zdim
yesterday
@zdim...yes, correctly understood. Each line has 10 columns, but i need to form the hash out of column 1 (key) and column 7 (value)....yes that symbolic reference aspect did come, but i wasn't sure how to apply it here... :(
– SangramJB
yesterday
OK, thank you. The symbolic reference business -- you don't want it. Just don't do it. Now, your explanation says that "contents of the first column from all files (sorted n uniq-ed) is reference value" -- how is that? The "first column" from all files is a long list of values, how is that a reference? Can you show a sample?
– zdim
yesterday
OK...now i have files like : exp1.txt, exp2.txt, exp3.txt so on. To collect and create a list of all reference values I do this :cut -f 1 *.txt | sort | uniq > ref_list.txt
– SangramJB
yesterday
You are trying to use symbolic reference, to create variable names dynamically; it's only trouble and (almost) never necessary. Why not simply put references to those hashes in an array?
– zdim
yesterday
You are trying to use symbolic reference, to create variable names dynamically; it's only trouble and (almost) never necessary. Why not simply put references to those hashes in an array?
– zdim
yesterday
The hash is made with key of the first element (
$d[0]
) and value of ... the eight-th ($d[7]
) -- is that intended?– zdim
yesterday
The hash is made with key of the first element (
$d[0]
) and value of ... the eight-th ($d[7]
) -- is that intended?– zdim
yesterday
@zdim...yes, correctly understood. Each line has 10 columns, but i need to form the hash out of column 1 (key) and column 7 (value)....yes that symbolic reference aspect did come, but i wasn't sure how to apply it here... :(
– SangramJB
yesterday
@zdim...yes, correctly understood. Each line has 10 columns, but i need to form the hash out of column 1 (key) and column 7 (value)....yes that symbolic reference aspect did come, but i wasn't sure how to apply it here... :(
– SangramJB
yesterday
OK, thank you. The symbolic reference business -- you don't want it. Just don't do it. Now, your explanation says that "contents of the first column from all files (sorted n uniq-ed) is reference value" -- how is that? The "first column" from all files is a long list of values, how is that a reference? Can you show a sample?
– zdim
yesterday
OK, thank you. The symbolic reference business -- you don't want it. Just don't do it. Now, your explanation says that "contents of the first column from all files (sorted n uniq-ed) is reference value" -- how is that? The "first column" from all files is a long list of values, how is that a reference? Can you show a sample?
– zdim
yesterday
OK...now i have files like : exp1.txt, exp2.txt, exp3.txt so on. To collect and create a list of all reference values I do this :
cut -f 1 *.txt | sort | uniq > ref_list.txt
– SangramJB
yesterday
OK...now i have files like : exp1.txt, exp2.txt, exp3.txt so on. To collect and create a list of all reference values I do this :
cut -f 1 *.txt | sort | uniq > ref_list.txt
– SangramJB
yesterday
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
The shown code attempts to use symbolic references to construct variable names at runtime. Those things can raise a lot of trouble and should not be used, except very occasionally in very specialized code.
Here is a way to read multiple files, each into a hash, and store them for later processing.
use warnings;
use strict;
use feature 'say';
use Data::Dump qw(dd);
my @files = @ARGV;
my @data;
for my $file (@files)
open my $fh, '<', $file or do
warn "Skip $file, can't open it: $!";
next;
;
push @data, map (split /t/, $_)[0,6] <$fh> ;
dd @data;
Each hash associates the first column with the seventh (index 6), as clarified, for each line. A reference to such a hash for each file, formed by
, is added to the array.
Note that when you add a key-value pair to a hash which already has that key the new overwrites the old. So if a string repeats in the first column in a file, the hash for that file will end up with the value (column 7) for the last one. The OP doesn't discuss possible duplicates of this kind in data files (only for the reference file), please clarify if needed.
The Data::Dump is used only to print; if you don't wish to install it use core Data::Dumper
.
I am not sure that I get the use of that "reference file", but you can now go through the array of hash references for each file and fetch values as needed. Perhaps like
open my $fh_ref, '<', $ref_file or die "Can't open $ref_file: $!";
while (my $line = <$fh_ref>)
my $key = ... # retrieve the key from $line
print "$key: ";
foreach my $hr (@data)
print "$hr->$key ";
say '';
This will print key:
followed by values for that string, one from each file.
@SangramJB Edited code (it was splitting on any space, from my tests, now it uses tab), and added some notes.
– zdim
yesterday
you aren't using@files
in your code
– showaltb
yesterday
@showaltb Ah, thank you -- fixed
– zdim
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The shown code attempts to use symbolic references to construct variable names at runtime. Those things can raise a lot of trouble and should not be used, except very occasionally in very specialized code.
Here is a way to read multiple files, each into a hash, and store them for later processing.
use warnings;
use strict;
use feature 'say';
use Data::Dump qw(dd);
my @files = @ARGV;
my @data;
for my $file (@files)
open my $fh, '<', $file or do
warn "Skip $file, can't open it: $!";
next;
;
push @data, map (split /t/, $_)[0,6] <$fh> ;
dd @data;
Each hash associates the first column with the seventh (index 6), as clarified, for each line. A reference to such a hash for each file, formed by
, is added to the array.
Note that when you add a key-value pair to a hash which already has that key the new overwrites the old. So if a string repeats in the first column in a file, the hash for that file will end up with the value (column 7) for the last one. The OP doesn't discuss possible duplicates of this kind in data files (only for the reference file), please clarify if needed.
The Data::Dump is used only to print; if you don't wish to install it use core Data::Dumper
.
I am not sure that I get the use of that "reference file", but you can now go through the array of hash references for each file and fetch values as needed. Perhaps like
open my $fh_ref, '<', $ref_file or die "Can't open $ref_file: $!";
while (my $line = <$fh_ref>)
my $key = ... # retrieve the key from $line
print "$key: ";
foreach my $hr (@data)
print "$hr->$key ";
say '';
This will print key:
followed by values for that string, one from each file.
@SangramJB Edited code (it was splitting on any space, from my tests, now it uses tab), and added some notes.
– zdim
yesterday
you aren't using@files
in your code
– showaltb
yesterday
@showaltb Ah, thank you -- fixed
– zdim
yesterday
add a comment |
up vote
2
down vote
The shown code attempts to use symbolic references to construct variable names at runtime. Those things can raise a lot of trouble and should not be used, except very occasionally in very specialized code.
Here is a way to read multiple files, each into a hash, and store them for later processing.
use warnings;
use strict;
use feature 'say';
use Data::Dump qw(dd);
my @files = @ARGV;
my @data;
for my $file (@files)
open my $fh, '<', $file or do
warn "Skip $file, can't open it: $!";
next;
;
push @data, map (split /t/, $_)[0,6] <$fh> ;
dd @data;
Each hash associates the first column with the seventh (index 6), as clarified, for each line. A reference to such a hash for each file, formed by
, is added to the array.
Note that when you add a key-value pair to a hash which already has that key the new overwrites the old. So if a string repeats in the first column in a file, the hash for that file will end up with the value (column 7) for the last one. The OP doesn't discuss possible duplicates of this kind in data files (only for the reference file), please clarify if needed.
The Data::Dump is used only to print; if you don't wish to install it use core Data::Dumper
.
I am not sure that I get the use of that "reference file", but you can now go through the array of hash references for each file and fetch values as needed. Perhaps like
open my $fh_ref, '<', $ref_file or die "Can't open $ref_file: $!";
while (my $line = <$fh_ref>)
my $key = ... # retrieve the key from $line
print "$key: ";
foreach my $hr (@data)
print "$hr->$key ";
say '';
This will print key:
followed by values for that string, one from each file.
@SangramJB Edited code (it was splitting on any space, from my tests, now it uses tab), and added some notes.
– zdim
yesterday
you aren't using@files
in your code
– showaltb
yesterday
@showaltb Ah, thank you -- fixed
– zdim
yesterday
add a comment |
up vote
2
down vote
up vote
2
down vote
The shown code attempts to use symbolic references to construct variable names at runtime. Those things can raise a lot of trouble and should not be used, except very occasionally in very specialized code.
Here is a way to read multiple files, each into a hash, and store them for later processing.
use warnings;
use strict;
use feature 'say';
use Data::Dump qw(dd);
my @files = @ARGV;
my @data;
for my $file (@files)
open my $fh, '<', $file or do
warn "Skip $file, can't open it: $!";
next;
;
push @data, map (split /t/, $_)[0,6] <$fh> ;
dd @data;
Each hash associates the first column with the seventh (index 6), as clarified, for each line. A reference to such a hash for each file, formed by
, is added to the array.
Note that when you add a key-value pair to a hash which already has that key the new overwrites the old. So if a string repeats in the first column in a file, the hash for that file will end up with the value (column 7) for the last one. The OP doesn't discuss possible duplicates of this kind in data files (only for the reference file), please clarify if needed.
The Data::Dump is used only to print; if you don't wish to install it use core Data::Dumper
.
I am not sure that I get the use of that "reference file", but you can now go through the array of hash references for each file and fetch values as needed. Perhaps like
open my $fh_ref, '<', $ref_file or die "Can't open $ref_file: $!";
while (my $line = <$fh_ref>)
my $key = ... # retrieve the key from $line
print "$key: ";
foreach my $hr (@data)
print "$hr->$key ";
say '';
This will print key:
followed by values for that string, one from each file.
The shown code attempts to use symbolic references to construct variable names at runtime. Those things can raise a lot of trouble and should not be used, except very occasionally in very specialized code.
Here is a way to read multiple files, each into a hash, and store them for later processing.
use warnings;
use strict;
use feature 'say';
use Data::Dump qw(dd);
my @files = @ARGV;
my @data;
for my $file (@files)
open my $fh, '<', $file or do
warn "Skip $file, can't open it: $!";
next;
;
push @data, map (split /t/, $_)[0,6] <$fh> ;
dd @data;
Each hash associates the first column with the seventh (index 6), as clarified, for each line. A reference to such a hash for each file, formed by
, is added to the array.
Note that when you add a key-value pair to a hash which already has that key the new overwrites the old. So if a string repeats in the first column in a file, the hash for that file will end up with the value (column 7) for the last one. The OP doesn't discuss possible duplicates of this kind in data files (only for the reference file), please clarify if needed.
The Data::Dump is used only to print; if you don't wish to install it use core Data::Dumper
.
I am not sure that I get the use of that "reference file", but you can now go through the array of hash references for each file and fetch values as needed. Perhaps like
open my $fh_ref, '<', $ref_file or die "Can't open $ref_file: $!";
while (my $line = <$fh_ref>)
my $key = ... # retrieve the key from $line
print "$key: ";
foreach my $hr (@data)
print "$hr->$key ";
say '';
This will print key:
followed by values for that string, one from each file.
edited 19 hours ago
answered yesterday
zdim
30.9k32040
30.9k32040
@SangramJB Edited code (it was splitting on any space, from my tests, now it uses tab), and added some notes.
– zdim
yesterday
you aren't using@files
in your code
– showaltb
yesterday
@showaltb Ah, thank you -- fixed
– zdim
yesterday
add a comment |
@SangramJB Edited code (it was splitting on any space, from my tests, now it uses tab), and added some notes.
– zdim
yesterday
you aren't using@files
in your code
– showaltb
yesterday
@showaltb Ah, thank you -- fixed
– zdim
yesterday
@SangramJB Edited code (it was splitting on any space, from my tests, now it uses tab), and added some notes.
– zdim
yesterday
@SangramJB Edited code (it was splitting on any space, from my tests, now it uses tab), and added some notes.
– zdim
yesterday
you aren't using
@files
in your code– showaltb
yesterday
you aren't using
@files
in your code– showaltb
yesterday
@showaltb Ah, thank you -- fixed
– zdim
yesterday
@showaltb Ah, thank you -- fixed
– zdim
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237394%2fcreating-multiple-hashes-from-multiple-files-in-one-go%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You are trying to use symbolic reference, to create variable names dynamically; it's only trouble and (almost) never necessary. Why not simply put references to those hashes in an array?
– zdim
yesterday
The hash is made with key of the first element (
$d[0]
) and value of ... the eight-th ($d[7]
) -- is that intended?– zdim
yesterday
@zdim...yes, correctly understood. Each line has 10 columns, but i need to form the hash out of column 1 (key) and column 7 (value)....yes that symbolic reference aspect did come, but i wasn't sure how to apply it here... :(
– SangramJB
yesterday
OK, thank you. The symbolic reference business -- you don't want it. Just don't do it. Now, your explanation says that "contents of the first column from all files (sorted n uniq-ed) is reference value" -- how is that? The "first column" from all files is a long list of values, how is that a reference? Can you show a sample?
– zdim
yesterday
OK...now i have files like : exp1.txt, exp2.txt, exp3.txt so on. To collect and create a list of all reference values I do this :
cut -f 1 *.txt | sort | uniq > ref_list.txt
– SangramJB
yesterday