Gossamer Forum
Home : General : Perl Programming :

Help With Opening a File for Use - Please Help!

Quote Reply
Help With Opening a File for Use - Please Help!
Can anyone help me?

Here is what I am trying to do:

open (DB, ">/databases/test.txt");
if ($db_use_flock) { flock(DB, 1); }

@data=<DB>;
close(DB);

foreach $line(@data) {
@ThisLine = split(/\|/,$line);
$Name = $ThisLine[2];
}

Example database format:

01|5|Joe Cool

I then call it using $Name.

Example: My name is $Name should print: My name is Joe Cool

However, I get nothing!

What am I doing wrong or what am I missing? This is driving me nuts and it is probably something simple!

By the way, for testing purposes, I have the databases directory and the test.txt permisssions set to 777, if that matters. Also, the database is only 1 line of delimited data.

Thanks!

Last edited by:

BrianYork: Jul 11, 2003, 6:39 AM
Quote Reply
Re: [BrianYork] Help With Opening a File for Use - Please Help! In reply to
Try testng the result of the open

open (DB, ">/databases/test.txt") or die "Error: $!";

You probably have the wrong path, or wrong permissions on the file
Quote Reply
Re: [BrianYork] Help With Opening a File for Use - Please Help! In reply to
How about something like;

Code:
open(DB,"/databases/test.txt") || die "Can't open test.txt. Reason: $!";
@data = <DB>;
close(DB);

foreach (@data) {

chomp;
my ($ID,$Var,$Name) = split("\|",$_);

# now you have something like;
# $ID => 01
# $Var => 5
# $Name => Joe Cool

... do whatever you want here ....

}

Just a bit more error checking, and a few line savers (although I'm sure Paul could do better Wink).

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] Help With Opening a File for Use - Please Help! In reply to
Mark and Andy,

Thanks!

Andy, you were right....I had to remove the first / in my database path. I did not even notice I had it there so I was never opening the databas!

Mark, thanks for your code. It works too and is another way to do it.

Good job!