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:
How do I for loop variables into a file?

 



DSelver
Novice

Apr 21, 2001, 2:53 PM

Post #1 of 7 (597 views)
How do I for loop variables into a file? Can't Post

Simple web form processing.

I'm tired of listing out every $FORM{'field'} that I want to write to a file. So I'm thinking, 'there's got to be a way to use a for loop to write out every variable to a file.' Here's what I've tried so far..


Code
       open(OUTDB,">>/var/home/user1/data.csv"); 
foreach $FORM(@FORM) {
print OUTDB "'$FORM',";
}
print "\n";
close(OUTDB);

Well, that writes absolutely nothing to the file. Of course I'm not sure of the syntax of the $'s and @'s, but I tried to give it a whirl before I posted.

So, two questions...
1) What is the correct syntax for what I'm trying to do..
and
2) Does putting the print "\n"; outside the loop effectively take care of making all of the parsed data go on one line?
Example:
field1,field2,field3
field1,field2,field3

or am I going to get..

field1,
field2,
field3,
field1,
field2,
field3,








zanardi
journeyman

Apr 21, 2001, 9:19 PM

Post #2 of 7 (591 views)
Re: How do I for loop variables into a file? [In reply to] Can't Post

ok well first you've got a hash, not an array so that is why your not getting any results:

1.

Code
open(F,'>>/var/home/user1/data.csv') or die $!; 
foreach (keys %FORM) {
print F "'$FORM{$_}',";
}
print "\n";
close F;

2. yes you were correct with that

----------------------
Fueled By ZCom


DSelver
Novice

Apr 21, 2001, 11:36 PM

Post #3 of 7 (590 views)
Re: How do I for loop variables into a file? [In reply to] Can't Post

WOW! You make it look so simple! Of course! I see it when you point it out.

So I revisited variables in my brand spankin new O'Reilly's Programming Perl 3rd Ed.....and visited the hash (keys / values) associations.

So if I wanted to play around with the value side of things..say to search for a value that needs to be excluded...say minimum age. (oh sure, I'd just look up the actual form field and compare...but just for the sake of my learning...)


Code
while ( ( value %FORM)) {  #for every instance of a value 
if '$_' <= '17' { #check that value to see if it's less than or equal to 17
print "you're too young"; #if so, then print message and...
die; #end of script
} #end of too young condition
else { #otherwise
## continue here ##
} # end of not too young condition
} # end of while

-Still waiting for that big perl programming click in the brain....DS>



zanardi
journeyman

Apr 22, 2001, 11:20 AM

Post #4 of 7 (579 views)
Re: How do I for loop variables into a file? [In reply to] Can't Post

ok well there is not hash function "value" but there is "values". And if im correct, and you run that through a while loop it's never going to stop, so If you wanted to check a certian element in your hash, I'd stick to the foreach:


Code
foreach (keys %FORM) { 
if ($_ eq 'min_age' && $FORM{$_} <= 17) {
print "Your too young";
}
}

I also came up with a new way to print your file:


Code
open(F,'>>/var/home/user1/data.csv') or die $!; 
print F "\n", map {"'$FORM{$_}',"} keys %FORM;
close F;

----------------------
Fueled By ZCom


japhy
Enthusiast

Apr 23, 2001, 7:40 AM

Post #5 of 7 (562 views)
Re: How do I for loop variables into a file? [In reply to] Can't Post

AGH! Linear searching over a hash is like clubbing someone to death with a loaded Uzi! (That was said by Larry Wall.)

To see if a hash element exists, just use the (dun dun DUN!) exists() function:


Code
if (exists $hash{$key}) { ... }

Instead of looping over the keys of the hash and seeing if it's the desired key, just use:


Code
if ($qualities{color} ne 'blue') { 
print "Sorry, you must get in blue.\n";
}

And CGI form processing is best left to CGI.pm.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


zanardi
journeyman

Apr 23, 2001, 12:08 PM

Post #6 of 7 (557 views)
Re: How do I for loop variables into a file? [In reply to] Can't Post


Code
if ($qualities{color} ne 'blue') {  print "Sorry, you must get in blue.\n";}

damn what was I thinking?

it's like a psychological thing, he used a loop so for some reason I felt it was necessary to do the same thing...

I've almost spent a whole week without doing any CGI... it's starting to leak out I can feel it


----------------------
Fueled By ZCom

(This post was edited by zanardi on Apr 23, 2001, 11:09 AM)


DSelver
Novice

Apr 26, 2001, 10:43 PM

Post #7 of 7 (543 views)
WELL BEAT THIS WITH YOUR UZI!! [In reply to] Can't Post

Ok. So I'm trying to take a stored file in the format of...

|field1|field2|field3|
|field1|field2|field3|
|field1|field2|field3|

and turn it into
field1
field2
field3
-------
field1
field2
field3
-------
field1
field2
field3
-------

ok.. so I'm for looping, but OBVIOUSLY I don't even have a UNloaded UZI.


Code
#! /usr/bin/perl -W 

$dataf = "/home/dselver/source.txt";
$datag = "/home/dselver/dest.txt";

open(new_data, ">> $datag") or die $!;
open(upddata, "<$dataf") or die $1;

while (><upddata> ){
(@res)=split(/\|/, $_);

foreach $i (@res) {
print newdata "$res($i)\n";
close(new_data);
}
print newdata "---------";
}
close(upddata);
close (new_data
die;

ok...hang on..
updated code..I was so close! fix a few file handles, change a split command supplied by Kanji in previous post. (I still don't understand where he got that split command and why it doesn't have to end with , $_) ), fix a variable and whoosh..

so here's what we have and it WORKS!

Code
#! /usr/bin/perl -W 

$dataf = "/home/dselver/source.txt";
$datag = "/home/dselver/dest.txt";

open(newdata, ">> $datag") or die $!;
open(upddata, "<$dataf");

while (><upddata> ){
(@res)=split m(\s*\|\s*);

foreach $i (@res) {
print newdata "$i\n";
close(new_data);

}
print newdata "---------"; #visual entry separator
}
close(upddata);

die;

oh, I guess I could've closed newdata, come to think of it. But hey, the perl script died.

phew.

ok...on with my report...in the REAL world..

See, the answer doesn't come to you until you ask the question. That's what my users are always telling me when the call me say, "How do you...oh..oh, never mind." -- Thanks for all your help.


 
 


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

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