help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

a try-if construction


From: Vic Norton
Subject: a try-if construction
Date: Tue, 9 Jun 2015 17:32:06 -0400

The perl code

   my @K= qw( 1   30   48 );
   my $n = 109;
   my ($k0, $k1);
   my $i = 0;
   for (my $i = 0; $i < @K; $i++) {
     $k0 = $K[$i];
     unless ($k1 = $K[$i + 1]) { $k1 = $n }
     printf("i = %d, k0 = %d, k1 = %d\n", $i + 1, $k0, $k1);
   }

and the octave code

   K = [ 1   30   48 ];
   n = 109;
   for i = 1 : length(K)
     k0 = K(i);
     try k1 = K(i + 1); end
     if k1 == k0; k1 = n; endif
     printf("i = %d, k0 = %d, k1 = %d\n", i, k0, k1);
   endfor

produce exactly the same results:

   i = 1, k0 = 1, k1 = 30
   i = 2, k0 = 30, k1 = 48
   i = 3, k0 = 48, k1 = 109

I am very comfortable with the "unless" line in perl. I am much less 
comfortable with the "try-if" lines in octave. Is this "try-if construction the 
best way to do what I want to do?

Regards,

Vic


reply via email to

[Prev in Thread] Current Thread [Next in Thread]