Home > Archive > PERL Beginners > April 2004 > Problem when use split
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem when use split
|
|
| John Doe 2004-04-29, 4:51 pm |
| Hello all,
i write script that open and read file but i have problem.
First, here are a script:
----
#!/usr/bin/perl
use DBI;
my $dbuser = "xxx";
my $dbpass = "xxx";
my $db = "xxx";
my $u_dbuser = "xxx";
my $u_dbpass = "xxx";
my $u_db = "xxx";
my $sth = "";
my $dbh = "";
my $result = "";
my $sql = "";
my $tree = "/mnt/ftp/ftp1/users/";
my $user = "";
my $filequota = ".ftpquota"; # file contain record format 0 0
my $temp = "";
$sql = "SELECT username FROM tbl_users";
$dbh = DBI->connect("DBI:mysql:$db", $dbuser, $dbpass) || die "MySQL is
down\n";
$sth = $dbh->prepare($sql);
$sth->execute;
$dbhnew = DBI->connect("DBI:mysql:$u_db", $u_dbuser, $u_dbpass) || die
"MySQL is down\n";
while(my @rows = $sth->fetchrow_array) {
$user = $rows[0];
$fulltree = $tree . $user . "/" . $filequota;
$files = 0;
$quotause = 0;
if (-e $fulltree) { # check to see if file exist
open(FILE, "$fulltree");
my @f = <FILE>;
close(FILE);
$temp = $f[0];
($files, $quotause) = split(/ /,$temp); # line 39
}
$sqlnew = "UPDATE tbl_users SET files='$files', quotause='$quotause' WHERE
username='$user'"; # line 41
$sthnew = $dbhnew->prepare($sqlnew);
$sthnew->execute;
}
$dbhnew->disconnect;
$dbh->disconnect;
----
When i run script i recive error:
# ./check-size.pl
: bad interpreter: No such file or directory
When i run perl with option -W say:
# perl -W check-size.pl
Use of uninitialized value in split at check-size.pl line 39.
Use of uninitialized value in concatenation (.) or string at check-size.pl
line 41.
Use of uninitialized value in concatenation (.) or string at check-size.pl
line 41.
Use of uninitialized value in split at check-size.pl line 39.
Use of uninitialized value in concatenation (.) or string at check-size.pl
line 41.
Use of uninitialized value in concatenation (.) or string at check-size.pl
line 41.
Any body can say me where is my error ?
Regards,
Condor
| |
| Casey West 2004-04-29, 4:51 pm |
| It was Thursday, April 29, 2004 when John Doe took the soap box, saying:
: Hello all,
: i write script that open and read file but i have problem.
: First, here are a script:
: ----
: if (-e $fulltree) { # check to see if file exist
: open(FILE, "$fulltree");
: my @f = <FILE>;
: close(FILE);
: $temp = $f[0];
: ($files, $quotause) = split(/ /,$temp); # line 39
: }
Sounds like your file is empty. You check that your file exists, and
that is good, but perhaps you should make sure the file is not empty
using '-s' instead of '-e'?
Casey West
--
"A new swimming pool is rapidly taking shape since the contractors
have thrown in the bulk of their workers."
--In an East African newspaper
| |
| Bob Showalter 2004-04-29, 4:51 pm |
| John Doe wrote:[color=darkred]
> ...
> When i run script i recive error:
> # ./check-size.pl
This can be caused by the file containing DOS-style line endings. If the
first line is:
#!/usr/bin/perl<CR><LF>
the kernel will include the <CR> as part of the file name and try to execute
"/usr/bin/perl<CR>", and will report that no such file exists.
| |
| John W. Krahn 2004-04-30, 1:17 am |
| John Doe wrote:
>
> Hello all,
Hello,
> i write script that open and read file but i have problem.
> First, here are a script:
> ----
> #!/usr/bin/perl
Change that to:
#!/usr/bin/perl -w
use strict;
> use DBI;
>
> my $dbuser = "xxx";
> my $dbpass = "xxx";
> my $db = "xxx";
>
> my $u_dbuser = "xxx";
> my $u_dbpass = "xxx";
> my $u_db = "xxx";
>
> my $sth = "";
> my $dbh = "";
> my $result = "";
> my $sql = "";
> my $tree = "/mnt/ftp/ftp1/users/";
> my $user = "";
> my $filequota = ".ftpquota"; # file contain record format 0 0
> my $temp = "";
>
> $sql = "SELECT username FROM tbl_users";
>
> $dbh = DBI->connect("DBI:mysql:$db", $dbuser, $dbpass) || die "MySQL is
> down\n";
> $sth = $dbh->prepare($sql);
> $sth->execute;
>
> $dbhnew = DBI->connect("DBI:mysql:$u_db", $u_dbuser, $u_dbpass) || die
> "MySQL is down\n";
> while(my @rows = $sth->fetchrow_array) {
> $user = $rows[0];
> $fulltree = $tree . $user . "/" . $filequota;
> $files = 0;
> $quotause = 0;
> if (-e $fulltree) { # check to see if file exist
> open(FILE, "$fulltree");
> my @f = <FILE>;
> close(FILE);
> $temp = $f[0];
> ($files, $quotause) = split(/ /,$temp); # line 39
> }
Change that if block to:
if ( open(FILE, $fulltree) and -s FILE ) {
local $_ = <FILE>;
close(FILE);
($files, $quotause) = split;
}
else {
warn "Cannot open $fulltree: $!";
next;
}
> $sqlnew = "UPDATE tbl_users SET files='$files', quotause='$quotause' WHERE
> username='$user'"; # line 41
> $sthnew = $dbhnew->prepare($sqlnew);
> $sthnew->execute;
> }
>
> $dbhnew->disconnect;
> $dbh->disconnect;
John
--
use Perl;
program
fulfillment
| |
| John Doe 2004-04-30, 9:06 am |
| "Bob Showalter" <Bob_Showalter@taylorwhite.com> wrote in message
news:2E4528861499D41199D200A0C9B15BC001D
7EF4E@FRISTX...
> John Doe wrote:
>
> This can be caused by the file containing DOS-style line endings. If the
> first line is:
>
> #!/usr/bin/perl<CR><LF>
>
> the kernel will include the <CR> as part of the file name and try to
execute
> "/usr/bin/perl<CR>", and will report that no such file exists.
Thank you, this is the problem.
I so many times delete shell line with vi and rewrite again...
and now after your post i open file with mc editor and i see that
contain after perl<CR><LF>. I removed and problem is resolved.
Regards,
John
| |
| John Doe 2004-04-30, 9:06 am |
|
"Casey West" <casey@g nest.com> wrote in message
news:20040429170302.GA69807@g nest.com...
> It was Thursday, April 29, 2004 when John Doe took the soap box, saying:
> : Hello all,
> : i write script that open and read file but i have problem.
> : First, here are a script:
> : ----
> : if (-e $fulltree) { # check to see if file exist
> : open(FILE, "$fulltree");
> : my @f = <FILE>;
> : close(FILE);
> : $temp = $f[0];
> : ($files, $quotause) = split(/ /,$temp); # line 39
> : }
>
> Sounds like your file is empty. You check that your file exists, and
> that is good, but perhaps you should make sure the file is not empty
> using '-s' instead of '-e'?
>
> Casey West
>
Thank you, i now use -s and problem with uninitialized value
is resolved.
Regards,
John
|
|
|
|
|