RE: help with defined and !not defined
> -Original Message- > From: lorid > Im want to set the $photo_year and $photo_month to another > value if it > is not already set. > > my $ARGV[0] will sometimes be passed a string like > this:nvIGRA with no > date at end and sometimes with date nvIGRA200511 > if it does not have date I need to set it another way but cant get > syntax of !defined > this will work: my $filename = $ARGV[0]; if ( $filename =~ /nvIGRA(\d{4})(\d{2})/ ) { my $photo_year=$1; my $photo_month=$2; } $photo_year=2005 unless defined $photo_year; $photo_month=7 unless defined $photo_month; if ($photo_month<10) { $photo_month="0".$photo_month; } note: if you can have other strings other than "nvIGRA" change that part into a regular expression. > > > my $filename = $ARGV[0]; > my $photo_year; > $photo_year= substr($filename,6,4); > my $photo_month ; > $photo_month= substr($filename,10,2); > #This just blows my web page > if(!defined $photo_year){ > $photo_year ="2005"; > } > > > #I have also tried > if(defined $qphoto_year){ >#test >$test1 ="Im defined"; > } > else{ > $test2 ="Im Not defined"; > print "$test2"; >} > > How come It always returns Im defined ,even when no date is > passed should I do some kind of checking on my $ARGV[0] > before doing the > asignment $photo_year= substr($filename,6,4); > > > help appreciated > > thanks > lori > ___ > Perl-Win32-Users mailing list > Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: help with defined and !not defined
On Fri, 22 Jul 2005 12:05:16 -0700, you wrote: >You want to check if it's null, not if it's defined. Defined means that the >variable exists. Almost. Defined means the variable exists AND its value is not undef, as this snippet demonstrates: my $foo; print !defined $foo; As for the OP's question, substr() does indeed return undef if the offset and length specification refers to something completely outside the string argument. I suspect that the elements of @ARGV have newlines or other whitespace at the end that are causing a problem. -- Eric Amick Columbia, MD ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: help with defined and !not defined
I see, that's a really bad example and I can see why it would confuse. Variables in perl don't have a true "undefined state" like say C might. Because of autovivification, *all* variables are in a state of potential existence. Once u reference a variable, it comes into existence with the undefined value. defined() see's if a variable has the undef value or something else, including null. So in ur example below it should be "a's value is not undef" not "a has a value". In perl "defined" means "has anything every been assigned to x". At 07:27 AM 7/22/05 -0700, lorid wrote: >you are right about I should test for null or length of value instead but >I found the place where I got information that says that to be defined a >var must have a value: >Beginnng Perl by Simon Cozens pg 123: > >"We can test if a variable is defined by... >my $a; >my $b; >$b = 10; >if (defined $a){ > print "a has a value.\n"; >} >if (defined $b){ > print "b has a value.\n"; >} >#only prints b has a value -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: help with defined and !not defined
Title: Re: help with defined and !not defined lorid wrote, on Fri 7/22/2005 10:27: my $a;: my $b;:: $b = 10;:: if (defined $a){ All well and good; but in your original post (which I no longer have available to quote, but I believe I recall correctly), your $a (or equivalent) was the result of a substr operation. So it has, in fact, been assigned a value (the desired portion of the string). What you *want* to test is if that portion is the zero-length string, hence the length() function is what you need in this case. Defined is for cases where nothing has actually been assigned, such as when a regex fails (or doesn't have enough matches to fill all the variables, or one alternation is chosen over another): /("[^"]*")|(.*)/; if (defined($1)) { #strip the quotes See? Good luck, Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300 fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years From: [EMAIL PROTECTED] on behalf of loridSent: Fri 7/22/2005 10:27To: Chris WagnerCc: perl-win32-usersSubject: Re: help with defined and !not defined Chris Wagner wrote:>You want to check if it's null, not if it's defined. Defined means that the>variable exists. You want something like if ($photo_year) {...}.>>At 01:48 PM 7/21/05 -0700, lorid wrote:> >>>my $ARGV[0] will sometimes be passed a string like this:nvIGRA with no>>date at end and sometimes with date nvIGRA200511>>if it does not have date I need to set it another way but cant get>>syntax of !defined>> >>>> >you are right about I should test for null or length of value instead butI found the place where I got information that says that to be defined avar must have a value:Beginnng Perl by Simon Cozens pg 123:"We can test if a variable is defined by...#!/usr/bin/perluse strict;#use warnings;my $a;my $b;$b = 10;if (defined $a){ print "a has a value.\n";}if (defined $b){ print "b has a value.\n";}#only prints b has a value___Perl-Win32-Users mailing listPerl-Win32-Users@listserv.ActiveState.comTo unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: help with defined and !not defined
Chris Wagner wrote: You want to check if it's null, not if it's defined. Defined means that the variable exists. You want something like if ($photo_year) {...}. At 01:48 PM 7/21/05 -0700, lorid wrote: my $ARGV[0] will sometimes be passed a string like this:nvIGRA with no date at end and sometimes with date nvIGRA200511 if it does not have date I need to set it another way but cant get syntax of !defined you are right about I should test for null or length of value instead but I found the place where I got information that says that to be defined a var must have a value: Beginnng Perl by Simon Cozens pg 123: "We can test if a variable is defined by... #!/usr/bin/perl use strict; #use warnings; my $a; my $b; $b = 10; if (defined $a){ print "a has a value.\n"; } if (defined $b){ print "b has a value.\n"; } #only prints b has a value ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: help with defined and !not defined
"if ($photo_year)" simply tests for a true value and will behave incorrectly if $photo_year is "0". What you really mean is "if (!length $photo_year)". -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Chris Wagner Sent: Thursday, July 21, 2005 3:03 PM To: perl-win32-users Subject: Re: help with defined and !not defined You want to check if it's null, not if it's defined. Defined means that the variable exists. You want something like if ($photo_year) {...}. At 01:48 PM 7/21/05 -0700, lorid wrote: >my $ARGV[0] will sometimes be passed a string like this:nvIGRA with no >date at end and sometimes with date nvIGRA200511 >if it does not have date I need to set it another way but cant get >syntax of !defined -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: help with defined and !not defined
You want to check if it's null, not if it's defined. Defined means that the variable exists. You want something like if ($photo_year) {...}. At 01:48 PM 7/21/05 -0700, lorid wrote: >my $ARGV[0] will sometimes be passed a string like this:nvIGRA with no >date at end and sometimes with date nvIGRA200511 >if it does not have date I need to set it another way but cant get >syntax of !defined -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
help with defined and !not defined
Im want to set the $photo_year and $photo_month to another value if it is not already set. my $ARGV[0] will sometimes be passed a string like this:nvIGRA with no date at end and sometimes with date nvIGRA200511 if it does not have date I need to set it another way but cant get syntax of !defined my $filename = $ARGV[0]; my $photo_year; $photo_year= substr($filename,6,4); my $photo_month ; $photo_month= substr($filename,10,2); #This just blows my web page if(!defined $photo_year){ $photo_year ="2005"; } #I have also tried if(defined $qphoto_year){ #test $test1 ="Im defined"; } else{ $test2 ="Im Not defined"; print "$test2"; } How come It always returns Im defined ,even when no date is passed should I do some kind of checking on my $ARGV[0] before doing the asignment $photo_year= substr($filename,6,4); help appreciated thanks lori ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs