• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

hash tables

I have a .dat file which contains many line of data
i have split the line up using split() and i am wanting to read line[5] which contains a name and line[1] which contains it's value.
i want to read them into a hash table, then output them to file

how do i store the 2 values into a hash table and output them to a file in the following format

name, value
name, value
name, value


I know very little about hash tables so any feedback will be appreciated
0
plennon
Asked:
plennon
1 Solution
 
rootkiddyCommented:
Below code is if you put the value's into a scalar.  If your input is in an array just wrap this with a for loop substituting $name with $name[$counter] and $value[$counter].  The for loop would look something like for($counter=0; $counter<@name; $counter++)


# This is where you split everything.
# $name is what I assumed you stored the name.
# $value is what I assumed you stored the value.

$input{"$name"} = $value;  # Creates a hash called input
                           # with the element your name
                           # retrieved with the the value
                           # that you retrieved

open(OUTPUT, ">name_of_outputfile") or die "Couldn't open the file for writing\n";

# Walk through each key in the hash input and print the key and the value in your format.
foreach $key (keys %input) {
   print OUTPUT "$key, $input{$key}\n";
}

close(OUTPUT);
0
 
ahoffmannCommented:
@line=split(/your-pattern/, $inputfromdat);
$input{$line[5]}=$line[1];
# output hash to file, see previous suggestion
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now