WizardOfPerl has asked for the wisdom of the Perl Monks concerning the following question:

For the last couple of days, I've been writing my first project in Perl.

A program that takes an array, bubble sorts through it's elements and

returns it with the contents in alphabetical order. I've been successful

so far, but the loop that actually bubble sorts through the array just

skipped altogether when run, with no errors.

-

I don't know why, I just want it to be fixed!

-

Here's the code. Please note that I know that the code is messy and

I know that the code could be written much more efficiently, but for

the moment, I just want it to be operational.

-

Here's the crucial bits:

# About halfway through the file my $alphabet = " abcdefghijklmnopqrstuvwxyz"; # Some more variables being defined here that I've left out my $alphabetised = "1"; until ($alphabetised = "1"){ # No code here, but when tested, it doesn't run until ($x >= (scalar @startingArray) - 1) { # minus 1 is necessary # etc etc etc this code doesn't run }; };

-

Please check the full code out here, yes it might give you eye cancer

https://raw.githubusercontent.com/IronGhost645/alphabetiser/master/alphabetiser.pl

Please don't roast me too hard

Replies are listed 'Best First'.
Re: Why doesn't this code run?
by LanX (Saint) on Jun 03, 2017 at 17:16 UTC
    Shouldn't it be  ( $alphabetised == "1" ) ?

    $alphabetised = "1" is always true (it's an assignment not a comparison)

    no wonder your code doesn't run.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Thanks, noted

      Any idea how to make the bubble sort work? It doesn't right now.

        Any idea how to make the bubble sort work?

        I'm sure lots of monks here have ideas about how to make a bubble-sort work, but the whole idea of using a bubble-sort rather than, e.g., the built-in sort is to learn about looping constructs, comparison operators, etc. Can you please post a short example of your non-working code (please see Short, Self-Contained, Correct Example) so we can comment?

        It doesn't right now.

        To restate the previous point, I don't see any bubble-sort right now. Rather than re-directing us to another site, please post code.

        Update: You might want to take a look at the perlfaq4 FAQ "How do I sort an array by (anything)?" and at List Processing, Filtering, and Sorting in our Tutorials.


        Give a man a fish:  <%-{-{-{-<

        I don't understand your code, maybe you meant while instead of until ?

        Or you need to start with my $alphabetized=0 ?

        otherwise your loop will never be entered

        Update:

        like anomalousmonk noted, which bubble sort? ? ?

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: Why doesn't this code run?
by dbander (Scribe) on Jun 04, 2017 at 01:07 UTC

    my $alphabetised = "1"; # Sets $alphabetised to "1" until ($alphabetised = "1"){ # Sets it to "1" again # Succeeds (returns TRUE) # TRUE before we even begin # Thus, never executed };

    Alternatives (pick one):

    until ($alphabetised == "1"){ # Checks instead of sets until ($alphabetised eq "1"){ # Checks instead of sets while ($alphabetised eq "1"){ # while() instead of until() my $alphabetised = "0"; # Initialize differently

A reply falls below the community's threshold of quality. You may see it by logging in.