473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Populating a hash from a file.

Kelicula
176 Expert 100+
salutations!
I am a self taught programmer. I am moderately competent with javascript, and moving into PERL.

I hope this isn't a stupid question but I have been googling, and reading tutorials for two days now, and i just can't seem to get this done.

I have a form(survey) that sends three things to a perlscript
1) the users IP
2) the time of day
3) the users choice (in the survey)

It can be seen here:
http://www.liveaudiomag.com/survey.shtml

If you make a choice and click submit, you will be taken to a page that displays the info gathered. I did that just to see if I was getting the info.

Now what I need is to put the ip, and time into a hash and then write it to a file.

for $i (param()){
%stuff = ("$i"=>"param($i)");
}

open (INFO, ">>results.txt");'

print INFO %stuff;


so that later I can scan the file for their ip if it's in there then i can see if they have voted within 24 hrs, and display a message (can't vote more than once during 24 hrs.)

if it's not there just write it.

ANYWAY I am having all kinds a problems.
I am using ActiveSate perl for win32 to test my script on my PC, the server I use is Linux based.

What I need is a "real person" to bounce ideas off and disscus this script with.
I am only asking for this as a one time thing. I think there are just some inherent missunderstandings here. If i can get this program to function properly. I will have conquered enough challanges to be able to write the scripts on my own from here out.

I just need a little help on this (my first) script?


anyone?

Anyone available (by email, chat, ect...) for a couple days of corresspondance to help get this newbie perl scripter off the ground?


thanks!!
Aug 19 '07 #1
6 3732
KevinADC
4,059 Expert 2GB
I will be glad to help, but here on this forum. I don't do chat or IM or email help.

Instead of doing something awkward like this:

Expand|Select|Wrap|Line Numbers
  1. for $i (param()){
  2. %stuff = ("$i"=>"param($i)");
  3. }
  4.  
  5. open (INFO, ">>results.txt");'
  6.  
  7. print INFO %stuff;
use the power of the CGI module. You can save form data directly to a file as is covered in the CGI documentation.

saving the state of a script to a file

This might or might not work "out-of-the-box" for your needs. If it does not you can always write yor own "logging" function to save data in the manner that best suits your requirements. Generally that would be a delimited file of some sort, CSV or other type of formatting.

Keep in mind, all perl based CGI sctipts must print/return some type of output to the browser, even if its a 204(?) no-content header and a blank page. Otherwise you will get a 500 internal server error as the default reply from the http server itself.
Aug 21 '07 #2
Kelicula
176 Expert 100+
Ah ha! Thanks. That has been happening (500 error).
I must look further into the CGI mod.

Ok thanks!
Aug 21 '07 #3
KevinADC
4,059 Expert 2GB
Also, your code would not have worked anyway:

Expand|Select|Wrap|Line Numbers
  1. for $i (param()){
  2.    %stuff = ("$i"=>"param($i)");
  3. }
param() is a function, when you double-quote a function call it is just treated like a literal string so if $i were 'cat' you would have ended up with a key/vaue pair like this:

cat = 'param(cat)'

instead of:

cat = 'black' (assuming 'black' was the value of the 'cat' field).

Plus the hash would have only had one key/value pair, the last one returned from looping through param(). You don't populate a hash the way you did, first you declare a hash then populate it with data:

Expand|Select|Wrap|Line Numbers
  1. my %stuff = ();
  2. for my $i (param()) {
  3.    $stuff{$i} = param($i);
  4. }
  5.  
Aug 22 '07 #4
Kelicula
176 Expert 100+
OK, I found out last night about the double quotes around param. Also I was having trouble with returning the "time" value because it holds spaces in it.
I made it work like this.
Expand|Select|Wrap|Line Numbers
  1. $entry = param(ip)."\t".param("time")."\t".param(choice)."\t";
I hand to quote the time then it returned the value. I am trying to create a DB of sorts. later I use:
Expand|Select|Wrap|Line Numbers
  1. print "$entry"."\n";
After opening my headers. I have been able to open a file, and append the new "entry" to it . Now I am worried though. This file "should" get extremely large. Also I am concerned with it being changed, while someone else is trying to access it. Can flock() help? or is that just for use within the package running.

I am planning on running through the file in a loop for pattern matching, to reduce overhead (memory) like:
Expand|Select|Wrap|Line Numbers
  1. foreach $line (@stuff){\n
  2. m/$ip/$line/;
  3. }
(that is very crude, but I'm just trying to show the concept)

But still I'm now wondering if this is even the best algorithum at all....

proably not.

any suggestions? I mean I can learn the code, but how should I "go about" accomplishing this?

Here is my current algorithum: (which I havn't completed yet)

gather ip, time, choice..
open file load old ip's and time's into hash. key= ip, value=time
scan file for ip, if found check time, see if greater or less than 24 hrs from current time.
if ip not found = append items to file, and proceed
if found and greater :
overwrite old time, and proceed
if found and less than:
disply error page; "can't vote more than once in 24 hrs";

the proceed part;

scan choices for matching results, create counters added a "click" each time certain choice is found.

create page saying thanks for voting.
so and so percent people voted for "whoever";
so and so people voting for "next guy";

ect...

Is this completely awkward? Like I say I have never done anything but a geustbook with perl before, and that was a LONG time ago.
I am really hoping this challange will, get me more experinced, and capable with perl.
It's like a project.

?????
Aug 23 '07 #5
KevinADC
4,059 Expert 2GB
OK, I found out last night about the double quotes around param. Also I was having trouble with returning the "time" value because it holds spaces in it.
I made it work like this.
Expand|Select|Wrap|Line Numbers
  1. $entry = param(ip)."\t".param("time")."\t".param(choice)."\t";
Puttng quotes around a form field name that does not have spaces in it does nothing for you. "time" is a single word it should not need any quotes.

I had to quote the time then it returned the value. I am trying to create a DB of sorts. later I use:
Expand|Select|Wrap|Line Numbers
  1. print "$entry"."\n";
After opening my headers. I have been able to open a file, and append the new "entry" to it . Now I am worried though. This file "should" get extremely large. Also I am concerned with it being changed, while someone else is trying to access it. Can flock() help? or is that just for use within the package running.
flock() can help but as far as I know, it is not fool-proof.

I am planning on running through the file in a loop for pattern matching, to reduce overhead (memory) like:
Expand|Select|Wrap|Line Numbers
  1. foreach $line (@stuff){\n
  2. m/$ip/$line/;
  3. }
(that is very crude, but I'm just trying to show the concept)

But still I'm now wondering if this is even the best algorithum at all....

proably not.

any suggestions? I mean I can learn the code, but how should I "go about" accomplishing this?
The way to process a large file without using too much memory is:

Expand|Select|Wrap|Line Numbers
  1. open(FH,"thefile") or die "$!";
  2. while(<FH>){
  3.    do something
  4. }
  5. close(FH);
or use the Tie::File module.

Here is my current algorithum: (which I havn't completed yet)

gather ip, time, choice..
open file load old ip's and time's into hash. key= ip, value=time
scan file for ip, if found check time, see if greater or less than 24 hrs from current time.
if ip not found = append items to file, and proceed
if found and greater :
overwrite old time, and proceed
if found and less than:
disply error page; "can't vote more than once in 24 hrs";

the proceed part;

scan choices for matching results, create counters added a "click" each time certain choice is found.

create page saying thanks for voting.
so and so percent people voted for "whoever";
so and so people voting for "next guy";

ect...

Is this completely awkward? Like I say I have never done anything but a geustbook with perl before, and that was a LONG time ago.
I am really hoping this challange will, get me more experinced, and capable with perl.
It's like a project.

?????
Hard to say if it is awkward. All you posted was a basic description of what you want to do, not an algorithim. It all depends on how you go about doing it.
Aug 24 '07 #6
Kelicula
176 Expert 100+
Hard to say if it is awkward. All you posted was a basic description of what you want to do, not an algorithim. It all depends on how you go about doing it.
Ok. I'll run it by you in a couple days. I'm going to attempt to finish ALL the operations. Thanks for your advice!

I definately wouldn't be at this point without you.

I'd still be trying to populate hashes like this:
Expand|Select|Wrap|Line Numbers
  1. for $i (param()){
  2. %hash{$i} = "param($i)";
  3. }
  4.  
heheheee
Aug 24 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Michael H | last post by:
Hi all, I guess I don't fully understand how a SHA1 hash value is calculated in C# / .NET for a large file... I'm trying to calculate SHA1 values for large files that are much larger than my...
1
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the...
6
by: pycraze | last post by:
Hi , I am using Fedora Core -3 and my Python version is 2.4 . kernel version - 2.6.9-1.667smp There is some freakish thing that happens with python hashes when i run a python script my...
1
by: John Wright | last post by:
I want to create a file hash on my exe files and store the hash signature in a database so I can retrieve the value to compare the file hash to ensure I have an untampered file. How can this be...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
2
by: =?Utf-8?B?TW91dGhPZk1hZG5lc3M=?= | last post by:
How can I add an MD5 hash to XMLSerializer.Serialize without corrupting the content of the file; then how to read it back to verify is correct? I'd like to code up something (see below) that...
3
by: Michael Bacarella | last post by:
The id2name.txt file is an index of primary keys to strings. They look like this: 11293102971459182412:Descriptive unique name for this record\n 950918240981208142:Another name for another...
5
by: nirmal1349 | last post by:
Hi, I have a hash in hash file in perl which looks some thing like this: $FIELDS = { 'abc' => { 'Description' => { 'Purpose' => 'some data is present',...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.