Solved

hash tables (cgi)

Posted on 2003-11-03
2
355 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
[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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl output collect 7 69
instmodsh - listing perl modules 2 91
Add additional column to .csv using Perl. 8 163
Control Number of Log Files -Perl 7 94
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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.…
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…

733 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