LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-14-2004, 02:47 PM   #1
nifflerX
Member
 
Registered: Dec 2003
Distribution: RedHat 8.0, 7.1 and Enterprise WS 3,4
Posts: 94

Rep: Reputation: 16
Bad switch statement (problem in the code block?)


Hi,

I'm running RHEL version 3 and have written a script in Perl. The bulk of the script is a large switch statement. When I run this with 134 cases it runs fine, when I run it with 136 cases I get the error:

Bad switch statement (problem in the code block?) near <filename> line ###

I can't see what's wrong with the code because the line 135 is exactly the same as line 134 except that it stats with 'case 135' instead of 'case 136'. I've posted the code below. Does anyone know what is going on here. Thanks so much.


Code:
#!/usr/bin/perl -w
                                                                                
use Switch;
                                                                                
open(IN,"filename");
open(OUT,">filename_out");
                                                                                
$count=1;
while(<IN>) {
  $i=1;
  chomp($_);
  print OUT "$_\n";
  switch ($count) {
        case 1 { for ($i=1; $i<=6; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
        case 2 { for ($i=1; $i<=1; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
        case 3 { for ($i=1; $i<=3; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
        case 4 { for ($i=1; $i<=1; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
        case 5 { for ($i=1; $i<=2; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
        case 6 { for ($i=1; $i<=3; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
        case 7 { for ($i=1; $i<=2; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
...
       case 134 { for ($i=1; $i<=3; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }  }
  if ($count == 135) { for ($i=1; $i<=6; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
  if ($count == 136) { for ($i=1; $i<=9; $i++) { print OUT "0\t0\t0\t0\t0\t0\n"; } }
  $count++;
}
close(IN);
close(OUT);
Note that the last two if statements were because lines 135 and 136 wouldn't work in the case statement.

I've just discovered a weird problem. If I take the program as is and move it to a central location so more than one user could use it I get the 'Bad switch' error even though I don't get this error when I have the script in my home directory. Does anyone have any ideas on what could be causing this script to be so temperamental? Thanks so much.


-NifflerX
 
Old 07-14-2004, 03:06 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Not really able to say X is wrong.... but... erm... 136 near identical switches?? that's just bizarre.... if it really does have to be quite so repetative, why not ditch the select and use an array to hold the number of inner loops?

Code:
my @iters = (0, 6, 1, 3, 1, 2, 3, 2, ... , 3, 6, 9);
...
print OUT "$_\n";
for ($i = 1; $i < $iters($count); $i++) { print ... }
Never seems much point troubleshooting bad code when it's easier to make good code in the first place... but then i'm sure even my solution is crude to others....

Last edited by acid_kewpie; 07-14-2004 at 03:08 PM.
 
Old 07-14-2004, 03:45 PM   #3
nifflerX
Member
 
Registered: Dec 2003
Distribution: RedHat 8.0, 7.1 and Enterprise WS 3,4
Posts: 94

Original Poster
Rep: Reputation: 16
Thank you so much. This proves yet again that there are many better programmers than me . Thank you again.


-NifflerX
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch statement on strings in structures? Mistro116@yahoo.com Programming 2 11-13-2005 07:18 PM
Switch Statement w/ Constant Variables Mistro116@yahoo.com Programming 1 10-04-2005 07:48 AM
how to close 'else' statement block in bash script servnov Linux - General 9 11-12-2004 03:53 PM
for statement c code question dragoon linux Programming 3 12-31-2003 11:17 AM
switch statement linuxanswer Programming 2 11-29-2003 11:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration