in reply to Simple array sorting

The problematic things that strike me are:

  1. It looks like you aren't using strict
  2. Ditto for warnings I bet
  3. Reusing $foo
  4. Using a foreach with a counter, which indicates this ought to be normal for loop
  5. Setting your counter to -1 and then incrementing the counter as the first thing in the loop, effectively starting at zero.
  6. If you know the indexes at which the letters exist that you want, why not simply copy those into a new array?
  7. How do you determine what you want to spell with the letters pulled from the array? How is this supposed to know how to arrange the letters?

What are you actually trying to accomplish FireBird34?

Replies are listed 'Best First'.
Re: Re: Simple array sorting
by FireBird34 (Pilgrim) on Jan 10, 2003 at 01:34 UTC
    The only purpose of this code is for the learning ability, in patching up a syntax error, and getting a code letter out of it (from a series of programs, a certain code will be found for something else). This program (I patched the error so no one will point it out ;)) will generate the letter "u".

    Now I know there is alot of useless garbage in the program, but that is the point -- I don't want the person to just look at a statement like 'print "u";', but throw something in there to make it look 'bigger' than it actually is... and yes, I know there is a commented line reguarding a random call -- that was on purpose.

    Anyway, as I said, this is for the purpose of finding an error in a piece of code, fixing it, and running it to obtain something. Here is the final result that is being used:
    #!/usr/bin/perl use strict; use warnings; my @a = 'a'..'z'; my $b = $a[19]; &blah(); srand('5' ^ my $blah1); my @c=split(/ */, "abcdefghijklmnopqrstuvwxyz"); my $a = $c[20]; for(my $i = 0) { @c = $c[20];#!/\^s/[int(rand(20))]/\^s/; print "Good job!\n\nCode Letter: @c"; } sub blah { my $blah = "a c e g i k m o q s u w y"; my @blah = split(/ /, $blah); my $i = -1; foreach $blah(@blah) { $i++; delete $blah[$i] unless $i eq 2 || $i eq 4 || $i eq 6; } $blah = join('', $b,$blah[4],$blah[6],$blah[2]); @blah = $blah; $blah1 = @blah; }

      Who is supposed to learn from this exercise? What are they supposed to learn? You've got a bitwise XOR seeding srand, rand, regular expressions, and the infrequently used int. At the same time, the surrounding code is blatantly obtuse: my declarations in the operations that use them, a braindead for loop, an even worse foreach, reuse of variable names. I mean, you've got high-level constructs surrounded by horrifically poor code. If this test is intended for someone relatively new to Perl, there is no way they are going to find some minor error in your code, because they're going to be too busy wondering what ^ means and what the difference is between / / and / */! On the other hand, if you're looking at a mid- to upper-midlevel Perl coder who actually knows what those things do, you're wasting their time having them wade through garbage that they ought to be smart enough not to write in the first place.

      Seriously, if you want someone to learn Perl, focus on proper use of the language, such as making smart choices regarding control statements and data types given certain needs. Save the tricks for entertainment purposes, because all the learner is going to get out of it is a feeling of bewilderment, but that is not the point of instruction.