CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
text file to hash??

 



airo
stranger

Apr 5, 2001, 6:40 PM

Post #1 of 4 (659 views)
text file to hash?? Can't Post

How do I…….?
I’m trying to get the hash from the text file so I can get the values from each key
--one at a time—and write them to a new file with HTML stuff to display the info.
Each section within the new file(HTML) will be the same. So for example
print NEW "<input type=text name=image value= $hash{key}[value] size=4>", "\n";

Here is the script I have to this point:
The text file looks like this:

db0001 # this is a keyvalue I use to keep from having duplicates.
0001: # this is the first key of the hash
http://127.0.0.1/clients/test500/proofs/
0001
1
1
1
1
10
15
20
45
90
0.5
0.8
1
1.2
3.5db0001 # this is a keyvalue I use to keep from having duplicates.
db0002 # this is a keyvalue I use to keep from having duplicates.
0002: # this is the second key of the hash
http://127.0.0.1/clients/test500/proofs/
0002
2
2
2
2
20
30
40
90
180
1
1.6
2
2.4
7
db0002 # this is a keyvalue I use to keep from having duplicates.

This is the script I use to write the hash to the text file:

open(DB, ">>$db") || die "Can't open $db: $!";
print DB "$delkey", "\n"; #this is the key to check for dups.
foreach my $key (keys %hash){
print DB "$key: \n";
foreach my $value (@{$hash{$key}}){
print DB "$value\n";
}
}
print DB "$delkey", "\n"; #this is the key to check for dups.
close(DB);




airo
stranger

Apr 6, 2001, 9:05 AM

Post #2 of 4 (649 views)
Re: text file to hash?? [In reply to] Can't Post

I got the info from the text file and displayed it like this:

open(DB, "$db") || die "cant: $!";
@info=<DB>;
chomp @info;
close(DB);
open ( NEW, ">image" ) or die "Can't open image $!\n";
print NEW "@info\n";
close NEW or die "Can't close image $!\n";
print "$info[1]", "\n";

but now the problem is I need to be able to extract only the data between the
keys (db001 ….data….db001) in the text file each time the above script runs.
How can I save that data to an array. Any ideas would be great and may even
set a spark..
thanks




airo
stranger

Apr 6, 2001, 11:48 AM

Post #3 of 4 (646 views)
Re: text file to hash?? [In reply to] Can't Post

Is there a way to modify this script to save the data to an array
instead of removing it?
open(DB, "<db$filename") || die "cant open db$filename: $!";
@old=<DB>;
close(DB);
$orignal= join "", @old;
$orignal =~ s!$delkey.*$delkey!!s;
open(DB, ">db$filename") || die "cant open db$filename: $!";
print DB $orignal;
close(DB);
I've tried a few things but nothing works...........
thanks



airo
stranger

Apr 6, 2001, 6:37 PM

Post #4 of 4 (641 views)
Re: text file to hash?? [In reply to] Can't Post

never mind I got it........
just too simple..

open(DB, "db$filename") || die "cant open db$filename: $!";
@old=<DB>;
close(DB);
$image= join "", @old;
$image =~ m/$delkey.*$delkey/;
open(FILE, ">file") || die "cant open db$filename: $!";
print FILE $image;
close(FILE);




 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives