Solved

hash tables

Posted on 2003-11-03
2
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

691 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