Making a hash map from a records file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making a hash map from a records file
# 1  
Old 11-09-2010
Making a hash map from a records file

Greets again. Let me first say that this forum and all of you participants are amazing. You give of your time and knowledge to those of us who aspire and have not reached your level (yet). My thanks.

I have created a file of records and wish to make a hash map from it. Here is an example of what it might look like;
Code:
12345,ABCDE
23456,BCDEF
34567,CDEFG
45678,DEFGH

I am having a lot of trouble with escaping and using single or double quotes, etc., in making the hash map
Code:
$map{'12345'} = 'ABCDE';

Any and all assistance will be much appreciated.
Thanks all,
Rick

Last edited by Scott; 11-09-2010 at 02:01 PM.. Reason: Code tags
# 2  
Old 11-09-2010
That syntax looks correct, leaving me to guess that maybe the problem isn't in the use of the hash itself. What is your code?
# 3  
Old 11-09-2010
When you say hash map, do you mean array?

What shell are you using? I could only get your data to work using an associative array in ksh93.
# 4  
Old 11-09-2010
From the syntax, that can't really be anything but perl.
# 5  
Old 11-09-2010
Simple Perl Script to Make a Hash Table out of incoming, comma separated record

This code assumes your data is in an external file ( which I called dat.txt ), and that the data has two fields which are comma separated.

I did not make any attempt to golf it ( make brief ), because I don't know what you want to do with the data structure once you have it built.

Code:
open ( F, 'dat.txt' ) || die;

my $hashmap;
while (<F>) {

    chomp;
    my ($k, $v);
    ( $k, $v ) = split /,/, $_;
    $hashmap->{$k} = $v;

}
close F;

print map "$_: $hashmap->{$_}\n", sort keys %$hashmap;

Hope that helps

Last edited by deindorfer; 11-09-2010 at 02:39 PM.. Reason: spelling mistake
# 6  
Old 11-09-2010
I suppose I wasn't as clear as I could have been

I have found another way and require a little more help.
data.txt consists of records like
Code:
12345,ABCDE
23456,BCDEF
34567,CDEFG
45678.DEFGH

I run the following:
Code:
perl -pi -e "s/^/\{"\'"/" data.txt
perl -pi -e 's/^/\$map/' data.txt
perl -pi -e "s/\,/\'\} \= \'/" data.txt

and get:
Code:
$map{'12345'} = 'ABCDE

and need a single quote at the end, followed by semi-colon.

I realize that this is easy for you all but I'll be damned if I can get it right :-)

Thanks again,
Rick

---------- Post updated at 07:01 PM ---------- Previous update was at 03:22 PM ----------

Hi again. So, here is the really weird thing.
$ /usr/bin/perl -v

This is perl, v5.8.8 built for i386-linux-thread-multi.....

However, when I run
$ perl -pi -e 's/$/FOO/' bar.txt

and bar.txt contains
Code:
12345,ABCDE

I get
Code:
FOO45,ABCDE

Honest. What do I do? This is replacing the beginning of the line and not the end. I am confuzelled.

Thanks, Rick
Moderator's Comments:
Mod Comment Once again - please use code tags

Last edited by vgersh99; 11-09-2010 at 08:06 PM.. Reason: Code tags, please!
# 7  
Old 11-09-2010
Revised Perl Script Outputting Code Snippets to a text file

This seems like an odd thing to want to do, but here you go.

Code:
open ( F, "data.txt" ) || die;
open ( G, ">perlcode.txt" ) || die;

while (<F>) {

    chomp;
    my ($k, $v);
    ( $k, $v ) = split /,/, $_;
    my $str = '$map{\'' . $k . '\'} = \'' . $v . '\';' . "\n";
    print G $str;

}
close F;
close G;

Given the data you originally supplied the above code will create a new file called "perlcode.txt" which will contain this.

Code:
$map{'12345'} = 'ABCDE';
$map{'23456'} = 'BCDEF';
$map{'34567'} = 'CDEFG';
$map{'45678'} = 'DEFGH';

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to print hash of hash in table format

Hi, I have a hash of hash where it has name, activities and count i have data like this - $result->{$name}->{$activities} = $value; content of that are - name - robert tom cat peter activities - running, eating, sleeping , drinking, work i need to print output as below ... (3 Replies)
Discussion started by: asak
3 Replies

2. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

3. Shell Programming and Scripting

Dynamically parse BibTeX and create hash of hash

Hello gurus, Iam trying to parse following BibTex file (bibliography.bib): @book{Lee2000a, abstract = {Abstract goes here}, author = {Lee, Wenke and Stolfo, Salvatore J}, title = {{Data mining approaches for intrusion detection}}, year = {2000} } @article{Forrest1996, abstract =... (0 Replies)
Discussion started by: wakatana
0 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

6. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

7. Shell Programming and Scripting

hash map Illegal division by zero problem

Hi Everyone, a.txt line1;a;33 line1;c;22 line1;b;0 line1;a;55 a.pl #!/usr/bin/perl use strict; use warnings; my @sorted=(); my @tmp; my $FA; my @F; (0 Replies)
Discussion started by: jimmy_y
0 Replies

8. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

9. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question