PDA

View Full Version : Filehandles problem


Ido Perelmutter
04-25-2003, 06:36 PM
Hi again.
I have a problem with one of my CGI/Perl programs: I have created a program that (theoretically) lets you create several photo albums and add pictures to them. I use a text file to hold the names of the albums and every album created has a text file of its own to hold the (links to the) picutres in it.

I use the open function to open/read/write to/create the files. The thing is that the files are created, but from an unkown reason printing into the files doesn't work. The open mode is set to allow writing to the filehandle but it doesn't work. It actually did work one random time after lots of tries but never worked again since.

for example, my first open statement is:
open (CATS, "+>>$path/cats.txt")
or die "Can't create categories file: $!";

and when adding an album it should do:
print CATS "$parse{name}\n";

What might be the reason for the problem and how can I fix it?!

Thanks to anyone who helps.

YUPAPA
04-25-2003, 09:53 PM
open (CATS, "+>>$path/cats.txt") or die "Can't create categories file: $!";
print CATS "$parse{name}\n";
close(CATS);


It could be $path doesn't have a value so it writes to /cats.txt OR $parse{name} does not have a value...

I can't correct anything now because I don't know how the entire script works :o

Ido Perelmutter
04-26-2003, 02:00 PM
Originally posted by YUPAPA

open (CATS, "+>>$path/cats.txt") or die "Can't create categories file: $!";
print CATS "$parse{name}\n";
close(CATS);


It could be $path doesn't have a value so it writes to /cats.txt OR $parse{name} does not have a value...

I can't correct anything now because I don't know how the entire script works :o

Both of them have a value. The file is created in the correct folder (specified of course by $path) but nothing gets written into it... I have no idea why...

And about the script: When executing the script the open (CATS, "+>>$path/cats.txt") or die "Can't create categories file: $!"; statement is executed, creating the cats.txt file if it doesn't exist and openning it for reading/writing if it already exists. After that I can do several things: Add a category to the photo album - a function (subroutine more precisely) that gets the new category's name from a form and stores it in $parse{name}. Then the program prints the name into the cats.txt file, which now shouldn't be empty. It also creates a text file for the category itself (namely $path/$parse{name}.txt) which in this point remains empty until the add picture function is used (Doesn't matter how). The files are created good, the print function doesn't print. I'm stuck...

Thanks again for helping me out YUPAPA!

Ido Perelmutter
04-26-2003, 03:12 PM
OK you can cancel my previous message it works good now.
But I still have a problem. I want to substitute a text in the file with another text. I've tried going over the lines of the file using
while (<CATS>) {
if (<CATS> =~ m/the text i want/) {
<CATS> =~ s/the text i want/text substitution text/;
}
}
but it gives me an error.
How can I do that?

Thanks.

YUPAPA
04-26-2003, 07:13 PM
while (<CATS> ) {
chomp;
s/the text i want/text substitution text/;
}

Ido Perelmutter
04-27-2003, 01:40 PM
Originally posted by YUPAPA

while (<CATS> ) {
chomp;
s/the text i want/text substitution text/;
}


Problems, problems, problems. Oh well, I guess that's what programming is all about:

I tried your way, but it still doesn't work. The substitution doesn't occur. I tried to do some matches to see if the match occurs, and it does. The code now is (Note that this is a subroutine to add a picture to a category):

sub addpic {
parseform(); # A subroutine to get the input from an HTML form
# SOME STUFF HERE WHICH IS NOT RELATED #
while (<CATS>) {
if (m/$parse{catname}/) {
m/($parse{catname}::\d+.\d+.\d+) :: (\d+)/; # Please ignore those spaces right and left from the "::", the board thinks it's an emoticon
print STDOUT "number1: $1, number2: $2<br>";
$one = $1; $two = $2; $new = $2 + 1;
print STDOUT "one: $one, two: $two, new: $new<br>";
s/$one::$two/$one::$new/;
}
}
print "Picture added successfuly to category $parse{catname}! ($parse{catname} now has $new pictures)<br>";
print qq~<a href="$url/picturecg.pl?admin">Go back to admin</a><br>~;
}

As you can see the while statement checks every line of cats.txt if a part of it matches $parse{catname} (Whose value is recieved by the parseform() subroutine). If it does, that means that this line holds the information for the category entered by the user. The program then looks for the last digit in the line and stores it in variable $2. That digit(s) is the number of pictures in the category. I then substitute that number with the same number increased by one (Because one picture was added). Only problem is that the substitution doesn't occur.
The code as you can see has a lot of crap which is unneccessary but I only put it there to make sure that the matches actually occur, and that's why those weird print statements are there too. I don't really need them.

Any idea why the s/// doesn't occur?!

Keepin' with the "thanks" :thumbsup: .

YUPAPA
04-27-2003, 05:01 PM
I am not sure if this is what u r looking for~

Try:


#!/usr/bin/perl
use strict;

my $total = 0;
my $name_search = 'Name';


open(TEST,"<test.txt") or die print "Fail to read test.txt: $!\n";
while(<TEST>) {
chomp;
my($db_id,$db_name,$db_pic) = split(/\t/,$_);

if($db_name =~ /$name_search/) {
print "I found $name_search\n";
}

print join('|',"ID: $db_id","Name: $db_name","Pic: $db_pic")."\n";

$total++;
}
close(TEST);

print "Total Records: $total\n";


What $name_search does is a variable enable you to search the second field of each record. Example, while reading the database test.txt, it will look for 'Name' in each second field. For the first record, it will check if field 2 contains 'Name' and then jumps to the next record and check if field 2 contains 'Name'.


For test.txt. THis is a database and each field is seperated with a tab.


1 A Name ME.jpg
2 Something YOU.jpg

Ido Perelmutter
04-27-2003, 06:24 PM
Not exactly what I entended but under several modifications it certainly suits my needs and it works perfectly.

That's another thank I owe you :)