Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

hash tables

Posted on 2003-11-03
2
Medium Priority
?
232 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

618 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