Solved

hash tables (cgi)

Posted on 2003-11-03
2
319 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

9 Experts available now in Live!

Get 1:1 Help Now