468,103 Members | 1,378 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

whats wrong with my code?

can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just the number. whats wrong with my code?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use Fcntl;
  3.  
  4. @array = (a..z);
  5. srand;
  6. foreach (1..5) 
  7. {    
  8. $name = int(rand scalar(@array));    
  9. print $array[$name];    
  10. splice(@array,$name,1);    
  11. }
  12.  
  13. print "content-type: text/perl \n\n";
  14. sysopen (PERL, "$name.pl", O_RDWR|O_EXCL|O_CREAT, 0755);
  15. printf PERL '#!/usr/bin/perl';
  16. printf PERL "\n\n";
  17. printf PERL 'print "hello world!";';
  18. printf PERL "\n";
  19. printf PERL 'sleep(3);';
  20. close (PERL);
Nov 9 '07 #1
5 1941
KevinADC
4,059 Expert 2GB
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just the number. whats wrong with my code?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use Fcntl;
  3.  
  4. @array = (a..z);
  5. srand;
  6. foreach (1..5) 
  7. {    
  8. $name = int(rand scalar(@array));    
  9. print $array[$name];    
  10. splice(@array,$name,1);    
  11. }
  12.  
  13. print "content-type: text/perl \n\n";
  14. sysopen (PERL, "$name.pl", O_RDWR|O_EXCL|O_CREAT, 0755);
  15. printf PERL '#!/usr/bin/perl';
  16. printf PERL "\n\n";
  17. printf PERL 'print "hello world!";';
  18. printf PERL "\n";
  19. printf PERL 'sleep(3);';
  20. close (PERL);
While your code is a bit awkward, it seems to work for me. $name is a random string of 5 letters followed by a number.
Nov 10 '07 #2
While your code is a bit awkward, it seems to work for me. $name is a random string of 5 letters followed by a number.
$name is a random string of 5 letters followed by a number, but at least for me, the files created lack the 5 letters. i tried adding a print "$name" and it printed 5 letters and a number though.
Nov 12 '07 #3
KevinADC
4,059 Expert 2GB
$name is a random string of 5 letters followed by a number, but at least for me, the files created lack the 5 letters. i tried adding a print "$name" and it printed 5 letters and a number though.

I have no idea why your system will not create a file with the letters and numbers. Maybe someone else will be able to shed some light on this odd problem.
Nov 13 '07 #4
I have no idea why your system will not create a file with the letters and numbers. Maybe someone else will be able to shed some light on this odd problem.
well thanks anyway, i'll try running it in ubuntu, since i am currently using windows xp. you said it did work on your system? and what os are you running?
Nov 13 '07 #5
KevinADC
4,059 Expert 2GB
well thanks anyway, i'll try running it in ubuntu, since i am currently using windows xp. you said it did work on your system? and what os are you running?

I didn't actually check to see if the code created a file. I only printed the random strings out to the screen. I assume it would create a file with the random string, I can't see any reason why it would not. I tested the code on my old windows 98 test server.
Nov 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Chris Geerdink | last post: by
4 posts views Thread by asdf | last post: by
1 post views Thread by aa | last post: by
3 posts views Thread by mahsa | last post: by
4 posts views Thread by blah | last post: by
5 posts views Thread by hiqu | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.