empty string in switch statement

John W. Eaton jwe at octave.org
Wed Jan 14 02:37:01 CST 2009


On 24-Nov-2008, Sergei Steshenko wrote:

| 
| 
| 
| --- On Mon, 11/24/08, Peter L. Søndergaard <peter at sonderport.dk> wrote:
| 
| > From: Peter L. Søndergaard <peter at sonderport.dk>
| > Subject: empty string in switch statement
| > To: bug-octave at octave.org
| > Date: Monday, November 24, 2008, 5:55 AM
| > The following script executes differently under Matlab and
| > Octave
| > 
| > 
| > f='';
| > 
| > switch(f)
| >  case ''
| >   disp('Equals.');
| > end;
| > 
| > 
| > In Matlab, the case statement matches and the word is
| > printed. In
| > Octave, there is no match.
| > 
| > I get the completely same behaviour if I use "case
| > []" instead.
| > 
| > I am using Octave 3.0.1 from the Fedora package
| > octave-3.0.1-1.fc9.x86_64
| > 
| > Matlab version 7.4.0.287 (R2007a)
| > 
| > Cheers,
| > 
| > Peter.
| > 
| > 
| > 
| I have slightly modified your code:
| 
| "
| 15> system("cat -n /home/sergei/acoustics_work/junk.m");
|      1  f='';
|      2
|      3  switch(f)
|      4  case ''
|      5    disp("Equals ''");
|      6  end
|      7
|      8
|      9  switch(f)
|     10  case []
|     11    disp("Equals []");
|     12  end
|     13
|     14  disp("test case 1 BEGIN");
|     15  disp('' == '');
|     16  disp("test case 1 END");
|     17
|     18
|     19  disp("test case 2 BEGIN");
|     20  disp('foo' == 'foo');
|     21  disp("test case 2 END");
| octave:16> source("/home/sergei/acoustics_work/junk.m");
| test case 1 BEGIN
| [](0x0)
| test case 1 END
| test case 2 BEGIN
|    1   1   1
| test case 2 END
| octave:17> ver
| ----------------------------------------------------------------------
| GNU Octave Version 3.0.3
| GNU Octave License: GNU General Public License
| Operating System: Linux 2.6.22.19-0.1-default #1 SMP 2008-10-14 22:17:43 +0200 i686
| ----------------------------------------------------------------------
| octave:18>                        
| "
| 
| - from the last two test cases it looks like comparison of zero length 
| strings is broken.

The correct result for comparing two empty character arrays is logical
([]).  If you want string comparison, use strcmp, not ==.

Jaroslav fixed the problem with using "" in switch cases.

jwe



More information about the Bug-octave mailing list