Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps

by pokemonk (Scribe)
on Jan 12, 2002 at 07:11 UTC ( [id://138206] : note . print w/replies, xml ) Need Help??


in reply to First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps

I find this really amazing, but i can't figure out why it happens. can someone please explain?

pokmon
  • Comment on Re: First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps

Replies are listed 'Best First'.
Re: Re: First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps
by blakem (Monsignor) on Jan 12, 2002 at 13:26 UTC
    From perlop:

      The auto-increment operator has a little extra builtin magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern /^[a-zA-Z]*[0-9]*$/, the increment is done as a string, preserving each character within its range, with carry:

        print ++($foo = '99'); # prints '100'
        print ++($foo = 'a0'); # prints 'a1'
        print ++($foo = 'Az'); # prints 'Ba'
        print ++($foo = 'zz'); # prints 'aaa'

      The auto-decrement operator is not magical.

    -Blake

Re: Re: First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps
by Anonymous Monk on Jan 12, 2002 at 07:35 UTC
    #!/usr/bin/perl use strict; my $theword = @ARGV[0]; my $A='a'; FINDIT: for (0..99999999){ $A++; if ($A eq "$theword") {print "$_\n"; last FINDIT; } }
      Wow. That can take a really long time to count. This little bit o' code is much faster.
      #!/usr/bin/perl -w # Convert pseudo-base26 number (digits: a-z) to decimal # really intended to find the ending number needed in # the for loop for any given string to be used my $num = 0; my $char = "a"; my $answer = 0; until ($char eq "aa") { $equiv{$char}=$num; $num++; $char++; } print "Enter your lowercase string: "; chomp($string = <STDIN>); @strArray=reverse(split(//,$string)); for ($i = 0; $i < @strArray; $i++) { $answer += ($equiv{$strArray[($i)]} * (25 ** $i)); } print --$answer;
        I tried the code and it did not work. After a little experimenting following code did work. I added the orignal one liner to the generator to validate it. The mistakes that I saw were two fold. First base should have be 26 not 25. Second the magical text increment behavior is not equivalent to incrementing numbers.
        9 goes to 10
        z goes to aa  not a0 
        
        as a result the equivs have to run a-z === 1-26 not 0-25 and the answer offset is 2 not 1.
        #!/usr/bin/perl -w # Convert pseudo-base26 number (digits: a-z) to decimal # really intended to find the ending number needed in # the for loop for any given string to be used my $num = 1; # was $num = 1 my $char = "a"; my $answer = 0; until ($char eq "aa") { $equiv{$char}=$num; $num++; $char++; } print "Enter your lowercase string: "; chomp($string = <STDIN>); @strArray=reverse(split(//,$string)); for ($i = 0; $i < @strArray; $i++) { $answer += ($equiv{$strArray[($i)]} * (26 ** $i)); # was 25 ** $i } $offset = $answer - 2; print $offset,"\n"; my $A="a";for(0..$offset){$A++;}print"$A";
Re: Re: First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps
by kiat (Vicar) on Jan 12, 2002 at 16:46 UTC
    hi pokemonk;

    In order to get an idea of how the word 'perl' gets printed, you may want to modify the code as follows:

    #!c:\perl\perl.exe -w use strict; my $A="a";for(0..285074){$A++; print"$A";}
    That is, you place the 'print "$A"' statement inside the loop. To see what what happens when, say, 1000 characters are printed, you stop the loop by pressing Ctr 'C' (if you're using Windows).

    kiat