Solved

hash tables (cgi)

Posted on 2003-11-03
2
341 Views
Last Modified: 2010-03-04
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

the file is then read and displayed as a web page

I know very little about hash tables so any feedback will be appreciated

0
Comment
Question by:plennon
2 Comments
 
LVL 8

Accepted Solution

by:
inq123 earned 100 total points
ID: 9672699
Hi plennon,

my %hash;
for my $file (@files) # process files
{
... # do your line splitting
my $name = $line[5]; # or some regex-extracted value
my $value = $line[1];
$hash{$name} = $value; # store the pair into hash

# now print everything out
foreach my $name (keys %hash) # keys gets all the keys for the %hash out
{
  print "$name,$hash{$name}\n"; # this prints out "name,value" for you as $value is stored in $hash{$name}
}

Cheers!
0
 
LVL 84

Expert Comment

by:ozo
ID: 9672794
my $datfile = "file.dat";
my $webfile="file.web";
open DATFILE,"<$datfile" or die "can't open $datfile because $!";
while( <DATFILE> ){
    my ($name,$value) = (split)[5,1];
    $hash{$name} = $value;
}
close DATFILE;
open OUTPUT,">$webfile" or die "Can't open $webfile because $!";
for( sort keys %hash ){
    print OUTPUT "$_,$hash{$_}<br>\n";
}
close OUTPUT;
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

810 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