Greatings and thanks for reading this! I run a free classifieds site and with the help of a friend, have added a few subroutines that create a new counter file that is named after the ad number (34.txt) and has a value of 0 to start. Now that all works fine, but ads posted prior to mods do not have a counter file. Rather than creating a new text file with a value of 0 for each ad (BIG JOB!), I thought I could write a script to do this for me. The script is supposed to:
  1. open the ads file
  2. flock it
  3. read each line into an array
  4. take just the first part of the array (the ad number), and create a new file for each, with a value of 0
  5. chmod each file to 0666
  6. close the ad file
  7. chmod it to 0666
  8. tell me that it is done
Here is the stuff:
#!/usr/bin/perl my $adfile = '/home/ebrae2/adskingston-www/cgi-bin/ebrae/data/ads.data +'; my $hits_dir = '/home/ebrae2/adskingston-www/cgi-bin/ebrae/hits/'; open(FILE, $adfile) || die "Can't open $adfile"; flock FILE, 2; while ($line = <FILE>) { chomp $line; my @counter_file = split(/\|/,$line); open (HITS_FILE, ">$hits_dir/$counter_file[0].txt") || die "Can't op +en $adfile"; ("$hits_dir/$counter_file[0].txt"); print HITS_FILE "0"; close (HITS_FILE); if ($os eq "unix") { chmod 0666, "$hits_dir/$$counter_file[0].txt"; } } close FILE; if ($os eq "unix") { chmod 0666, "$adfile"; } print "Content-type: text/html\n\n"; print "<BR><CENTER>"; print "<form><input type=button value=\"Success!\" onClick=\"locatio +n.href('http://www.adskingston.com')\"></FORM>";
I am new to this and know that there are probably some bugs. It does work, but only if there are 2 $$'s side by side on line 22, when I am telling it to flock each file it creates. AND, when I am connected via FTP (WS_FTP), run the script, and then try to open the "hits" folder, it doesn't show the new files, it hangs until I hit refresh... but it doesn't seem to be doing this for any other directories. Thought maybe it was just a fluke that the server was slowing down everytime I was trying to open that directory, maybe it is... the possibilities are endless!!!! Can someone tell me what I am doing wrong? Thanks! Edited by boo_radley for formatting

In reply to using loop/array to write files by ginocapelli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.