Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

testing if a value is empty

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hello,

I want to test if the value writing in the prompt is not empty;
i've use astructure control : unless but if i write no value and i press enter, in my file i found this
SYS=
or if no value is written , normally i can't exit from the structure control.
my code
Code:
#!/usr/bin/perl -w

use strict;

open FILE, ">./fic.cfg";
print "SYS Value = \n";
my $SYS=<stdin>;
unless ($SYS)
{
print "SYS Value =\n";
my $SYS=<stdin>;
}
print FILE "SYS=$SYS\n";

thanks
 
There's a few things you can do depending on what you mean by "empty":

Code:
# If empty means not defined
my $var;
# or: my $var = undef;
if (defined $var) {
   print "var was defined\n";
}
else {
   print "var wasn't defined\n";
}

# if empty means blank value
my $var = '';

# you can check with length
if (length $var == 0) {

-------------
Kirsle.net | Kirsle's Programs and Projects
 
On another note, 'defined' will return true if the variable has a blank value, ie

Code:
my $var = '';
if (defined $var) # should return true

-------------
Kirsle.net | Kirsle's Programs and Projects
 
you probably want something more like this:

Code:
#!/usr/bin/perl -w
use strict;
my $SYS = undef;
while (!$SYS) {
   print "SYS Value = \n";
   chomp($SYS=<STDIN>);
}
open FILE, ">./fic.cfg";
print FILE "SYS=$SYS\n";
close (FILE);



- Kevin, perl coder unexceptional!
 
i've this script
Code:
if ($valid ne "o")
{


do {
    print $_[1]."[".$_[0]."]";
    $_[0] = <stdin>;
    print "\n"; 	
    chomp($_[0]);
    
   } while (! ($_[0]));

}
else
{
print $_[1]."[".$_[0]."]";
    
   $_[0] = <stdin>;
   
   if (! ($_[0]))
	{ 
	$_[0] = $old; 
	} 
    print "\n";

    chomp($_[0]);

}

this part of the script don't execute as i want
Code:
else
{
print $_[1]."[".$_[0]."]";
    
   $_[0] = <stdin>;
   
   if (! ($_[0]))
	{ 
	$_[0] = $old; 
	} 
    print "\n";

    chomp($_[0]);

}

what i want is :
if there no values are taking in the prompt (<stdin>);i want
$_[0] equal to $old;
But when i run the script,if i take no value , i've not the old value;
My test seem not run good
(! ($_[0]))

??
 
i've resolve this
with something like this
Code:
$_[0] = <stdin>;
  
   $value = $_[0];
   
   if ((length($new)-1) == 0)
	{ 

	 $_[0] = $old; 

	}
 
heu
this is good
Code:
$_[0] = <stdin>;
  
   $value1 = $_[0];
   
   if ((length($value1)-1) == 0)
    { 

     $_[0] = $old; 

    }
 
The reason $_[0] has a length of 1 is because when you hit enter in <STDIN>, it puts a newline character (\n) at the end of whatever you typed. If you typed nothing, then it will return just "\n"

So chomp the variable. Chomp gets rid of newline characters from the end of a variable.

Code:
$_[0] = <stdin>;
chomp $_[0];

or even better

Code:
chomp ($_[0] = <STDIN>);

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top