Re: file test doesn't seem to be working

2002-02-05 Thread John Mooney


>>> "Brett W. McCoy" <[EMAIL PROTECTED]> 2/5/2002 10:35:48 AM >>>
>
>That's just more a matter of style -- I get the willies when subroutines
>modify global values invisibly.


Great advice. thanks


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-05 Thread Brett W. McCoy

On Tue, 5 Feb 2002, John Mooney wrote:

> Actually, it is not an incorrect way I believe, so much as he is using
> slightly incorrect syntax.  From the Nutshell ...

I really meant 'incorrect' in its usage, not the syntax -- he was using it
in a void context.

> ... I think the key is that he was not using a token that resulted in an
> explicit evaluation, returning & capturing a result. By wrapping the 2nd
> and 3rd tokens of the ternary clause in parens, this result can be
> acheived.
>
> Here's an example I use often in CGIs to accomplish background-color
> swapping in HTML table rows - achieiving an old computer-paper,
> green/white effect:
>
> local $swapper = -1;  # globals
> local $bgcolor   = '';
>
> sub swapBGcolor {
>(($swapper *= -1) > 0 ) ? ($bgcolor="#FF") : ($bgcolor="#F5DCC7");
> }

That can be dangerous, IMHO, modifying globals like that (I know it would
cause some problems in mod_perl!)  I would instead code it so that the
little subroutine returns the value you need:

$bgcolor = swapBGcolor();

sub swapBGcolor {

return ($swapper *= -1) > 0 ? '#FF' : '#F5DCC7'
}

That's just more a matter of style -- I get the willies when subroutines
modify global values invisibly.

-- Brett
  http://www.chapelperilous.net/

The English have no respect for their language, and will not teach
their children to speak it.
-- G. B. Shaw


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-05 Thread John Mooney

>>> "Brett W. McCoy" <[EMAIL PROTECTED]> 2/4/2002 9:28:42 PM >>>
>>On Mon, 4 Feb 2002, david wright wrote:
>>
>> i can't use the ternary operator like this? (damn waste if not) thanks.
>>
>> foreach $dup (@array){
>> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
>> )
>
>Yes, that is an incorrect way to use the ?: operator -- the operataor is
>handed an expression that yields a boolean value, and it returns a value
>based on what that boolean value is:
>
> Here is a one liner that accomplishes what you want:
>
>$ perl -e '$ans = (-d "/usr/bin") ? "yes\n" : "no\n"; print $ans'
>yes
>

Actually, it is not an incorrect way I believe, so much as he is using slightly 
incorrect syntax.  From the Nutshell ...


Ternary ?: is the conditional operator. It works much like an if-then-else statement, 
but it can safely be embedded within other operations and functions. 

test_expr ? if_true_expr : if_false_expr
If the test_expr is true, only the if_true_expr is evaluated. Otherwise, only the 
if_false_expr is evaluated. Either way, the value of the  _evaluated_expression_  
becomes the value of the entire expression.


 I think the key is that he was not using a token that resulted in an explicit 
evaluation, returning & capturing a result. By wrapping the 2nd and 3rd tokens of the 
ternary clause in parens, this result can be acheived.

Here's an example I use often in CGIs to accomplish background-color swapping in HTML 
table rows - achieiving an old computer-paper, green/white effect:

local $swapper = -1;  # globals
local $bgcolor   = '';   

sub swapBGcolor {
   (($swapper *= -1) > 0 ) ? ($bgcolor="#FF") : ($bgcolor="#F5DCC7");
}

Each time swapBGcolor is invoked, the value of $swapper is multiplied by -1, flipping 
between +1 or -1.  When that var is positive, the TRUE expression of the ternary 
opertaor is 'evaluated', assigning a value to $bgcolor at the same time. When $swapper 
is negative (false), the 2nd value of the global $bgcolor is assigned during the 
'evaluation'.

I remember struggling with getting this to work and finally hitting upon using parens 
to capture the result of an expression and returning it to the ? operator's result.

FYI
- John




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread Jeff 'japhy' Pinyan

On Feb 4, Brett W. McCoy said:

>On Mon, 4 Feb 2002, david wright wrote:
>
>> i can't use the ternary operator like this? (damn waste if not) thanks.
>>
>> foreach $dup (@array){
>> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
>> )
>
>Yes, that is an incorrect way to use the ?: operator -- the operataor is
>handed an expression that yields a boolean value, and it returns a value
>based on what that boolean value is:

That's not to say his use is incorrect.

  EXPR ? EXPR : EXPR

His code fits...

  (-d $dup) ? (print "yes: $dup\n") : (print "no:  $dup\n")

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread Brett W. McCoy

On Mon, 4 Feb 2002, david wright wrote:

> i can't use the ternary operator like this? (damn waste if not) thanks.
>
> foreach $dup (@array){
> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
> )

Yes, that is an incorrect way to use the ?: operator -- the operataor is
handed an expression that yields a boolean value, and it returns a value
based on what that boolean value is:

 Here is a one liner that accomplishes what you want:

$ perl -e '$ans = (-d "/usr/bin") ? "yes\n" : "no\n"; print $ans'
yes

-- Brett
  http://www.chapelperilous.net/

"The Lord gave us farmers two strong hands so we could grab as much as
we could with both of them."
-- Joseph Heller, "Catch-22"


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread John W. Krahn

David Wright wrote:
> 
> i am sending @array a directory (i.e. example /usr/dw5) which contains a
> lot of files and folders (directories).
> (i have already checked the value of @array and $dup and they are as
> desired.) What i want to accomplish is: print yes, "$dup (file name)" if
> it's a directory (folder) i am testing it on a directory which is
> composed of 90% folders. The example below will only print yes for .
> and .. and "no" for everything else. I have tried it with other file
> test's (i.e. -f, -A, -e, etc,...) none of them work as desired,... maybe
> i can't use the ternary operator like this? (damn waste if not) thanks.
> 
> foreach $dup (@array){
> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
> )


This works fine on my Linux system.  BTW this is usually written as:

print -d $dup ? 'yes: ' : 'no: ', "$dup\n";


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread david wright


On Monday, February 4, 2002, at 07:26 PM, Jeff 'japhy' Pinyan wrote:

> On Feb 4, david wright said:
>
>> i am sending @array a directory (i.e. example /usr/dw5) which 
>> contains a
>> lot of files and folders (directories).
>
> Be warned that readdir() returns NAMES, not PATHS.
>
>   opendir DIR, "/some/dir";
>   @files = readdir DIR;
>   closedir DIR;
>
> @files will contain (".", "..", "file1", "blah", ...).  Notice how those
> names do NOT have "/some/dir/" at the beginning?
>
>   for (@files) {
> print "$_ is ok\n" if -d "/some/dir/$_";
>   }
>
> --
> Jeff "japhy" Pinyan  [EMAIL PROTECTED]  
> http://www.pobox.com/~japhy/
> RPI Acacia brother #734   http://www.perlmonks.org/   
> http://www.cpan.org/
> ** Look for "Regular Expressions in Perl" published by Manning, in 
> 2002 **
>  what does y/// stand for?   why, yansliterate of 
> course.
>
>

Damn it, now it makes sense, exactly,... it's just reading a "string" it 
doesn't know what it's representing, that's why i have to chdir.   
(Guess that answers my question of my i would have to chdir if i can 
readdir ;-) ) excellent!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread Jeff 'japhy' Pinyan

On Feb 4, david wright said:

>i am sending @array a directory (i.e. example /usr/dw5) which contains a 
>lot of files and folders (directories).

Be warned that readdir() returns NAMES, not PATHS.

  opendir DIR, "/some/dir";
  @files = readdir DIR;
  closedir DIR;

@files will contain (".", "..", "file1", "blah", ...).  Notice how those
names do NOT have "/some/dir/" at the beginning?

  for (@files) {
print "$_ is ok\n" if -d "/some/dir/$_";
  }

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread david wright


On Monday, February 4, 2002, at 06:57 PM, Timothy Johnson wrote:

>
> Did you chdir to the directory?  Otherwise your script might be 
> checking the
> wrong directory.
>
> opendir(DIR,"/mydir");
> @array = readdir(DIR);
> chdir "/mydir";
> foreach $dup (@array){
> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
> }
>
>
well the return value of @array prints all the files in the desired 
directory, doesn't that mean that is the directory that the loop is 
operating on and -d is checking?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: file test doesn't seem to be working

2002-02-04 Thread Timothy Johnson


Did you chdir to the directory?  Otherwise your script might be checking the
wrong directory.

opendir(DIR,"/mydir");
@array = readdir(DIR);
chdir "/mydir";
foreach $dup (@array){
(-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
}

-Original Message-
From: david wright [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 04, 2002 5:51 PM
To: [EMAIL PROTECTED]
Subject: file test doesn't seem to be working


i am sending @array a directory (i.e. example /usr/dw5) which contains a 
lot of files and folders (directories).
(i have already checked the value of @array and $dup and they are as 
desired.) What i want to accomplish is: print yes, "$dup (file name)" if 
it's a directory (folder) i am testing it on a directory which is 
composed of 90% folders. The example below will only print yes for . 
and .. and "no" for everything else. I have tried it with other file 
test's (i.e. -f, -A, -e, etc,...) none of them work as desired,... maybe 
i can't use the ternary operator like this? (damn waste if not) thanks.

foreach $dup (@array){
(-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
)


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]