Solved

hash tables

Posted on 2003-11-03
2
225 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
Comment Utility
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
Comment Utility
@line=split(/your-pattern/, $inputfromdat);
$input{$line[5]}=$line[1];
# output hash to file, see previous suggestion
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now