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: Regular Expressions:
Is my file empty?

 



Vagabundo
New User

Feb 11, 2017, 11:02 AM

Post #1 of 6 (5418 views)
Is my file empty? Can't Post

Hello,
I must check if a txt-file is empty or not. But if there are only whitespaces in the file, it is empty for me.

I have tested:


Code
my $content = &ReadFile($datei);  
if($content =~ /\W/)
{ print "no letter - no numbers\n"; }
else
{ print "letters or/and numbers\n"; }
exit;


first try: newlines and spaces
second try: I add a letter

result: always no letters...
Even if I add the letter in the file

Can anybody help?
Chris


Laurent_R
Veteran / Moderator

Feb 11, 2017, 1:48 PM

Post #2 of 6 (5413 views)
Re: [Vagabundo] Is my file empty? [In reply to] Can't Post

Where did you get your ReadFile subroutine from? What does it do?

Did you check that the $content variable really contains something useful and that it really contains the content of your file?


FishMonger
Veteran / Moderator

Feb 11, 2017, 1:52 PM

Post #3 of 6 (5413 views)
Re: [Vagabundo] Is my file empty? [In reply to] Can't Post

Show us your ReadFile() sub.


BillKSmith
Veteran

Feb 11, 2017, 8:05 PM

Post #4 of 6 (5400 views)
Re: [Vagabundo] Is my file empty? [In reply to] Can't Post

I have created two test cases which match your descriptions. Your test produces the results that you report. Your test matches any non-word character. The leading space is a non-word character so the match succeeds in both cases.

I have replaced your test with one that tests for any non-whitespace character. If the match succeeds, the test returns false.


Code
use strict; 
use warnings;
for my $datei (0..1) {
my $content = ReadFile($datei);
#if($content =~ /\W/)
if($content !~ /[^\s]/)
{ print "no letter - no numbers\n"; }
else
{ print "letters or/and numbers\n"; }
}
exit;

sub ReadFile {
my @data = (
" \n \n \n",
" \n A\n \n",
);
return $data[shift];
}

OUTPUT:
no letter - no numbers
letters or/and numbers


This does not meet your spec exactly because characters that are neither whitespace nor word-characters (e.g. "!@$^*()_" ) would be treated the same as letters and numbers.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Feb 12, 2017, 3:33 PM

Post #5 of 6 (5391 views)
Re: [BillKSmith] Is my file empty? [In reply to] Can't Post

Yes, Bill, you nailed down the major problem in the original post. The regex is not good for the stated purpose. This would be already better:

Code
if ($content !~ /\w/)  {  
print "no letter - no numbers\n";
} else {
print "letters or/and numbers\n";
}

although I would probably reorganize it as something like this for better clarity:

Code
if($content =~ /\w/)  {  
print "letters or/and numbers\n";
} else {
print "no letter - no numbers\n";
}


This being said, we still don't know what the ReadFile function is doing.


Vagabundo
New User

Feb 13, 2017, 6:03 AM

Post #6 of 6 (5384 views)
Re: [BillKSmith] Is my file empty? [In reply to] Can't Post

 

 
 


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

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