[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

hash tables (cgi)

Posted on 2003-11-03
2
Medium Priority
?
375 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 400 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 85

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans
Suggested Courses

834 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