Solved

hash tables (cgi)

Posted on 2003-11-03
2
331 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERL get the value for query 4 145
ppm conversion to curl on a module install 8 80
Excel to CSV conversion with specific columns 5 74
Perl script to delete older files 6 73
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

25 Experts available now in Live!

Get 1:1 Help Now