Solved

hash tables

Posted on 2003-11-03
2
228 Views
Last Modified: 2013-12-25
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
Comment
Question by:plennon
2 Comments
 
LVL 2

Accepted Solution

by:
rootkiddy earned 125 total points
ID: 9673117
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
 
LVL 51

Expert Comment

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

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question