Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

string increment

by prad_intel (Monk)
on Sep 13, 2005 at 07:10 UTC ( #491497=perlquestion: print w/replies, xml ) Need Help??
prad_intel has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I tried out the following.
$sample='prad'; for($i=0;$i<=10;++$i){ ++$sample; } print"$sample";

The result for this = "prao"

I tried to increase the count beyond the last alphabet reaching z and it gives an output something like "prbc".

I notice that as the X in $i<=X is increased only the last two chareceters in the string "prad" is incremented.

What do i have to do to increment the whole string together?

Regards

Prad

Replies are listed 'Best First'.
Re: string increment
by Corion (Pope) on Sep 13, 2005 at 07:16 UTC

    I'm not sure what the result you want is. The following works for me:

    Q:\>perl -we "$s='corion'; for (1..1000) {$s++}; print $s" corkaz

    Maybe you need to increment by more than just 10, or maybe you want to clarify what you want to do. A different solution might be the following, which rotates all characters by one:

    Q:\>perl -we "$s='corion'; $s =~ tr[a-z][b-za]; print $s" dpsjpo

    Maybe give us the expected output, the output you get and the code you tried.

Re: string increment
by gargle (Hermit) on Sep 13, 2005 at 07:46 UTC

    Hi,

    It's base 26 aritmetic :)

    Base 10 aritmetic

    The following adds 1 ten times to the number 0. The output is 10.

    perl -e '$s=0;for $i (1..10) { ++$s }; print $s;'
    Base 26 aritmetic

    The following adds '1' 26 times to the 'number' 'a'. The output is 'aa'.

    perl -e '$s="a";for $i (1..26) { ++$s }; print $s;'
    --
    if ( 1 ) { $postman->ring() for (1..2); }
Re: string increment
by GrandFather (Sage) on Sep 13, 2005 at 07:30 UTC

    You may find the following instructive

    my $sample='prad'; ++$sample for(1..10); print "$sample";

    Perl is Huffman encoded by design.
Re: string increment
by prad_intel (Monk) on Sep 13, 2005 at 07:19 UTC
    Hi Corion and others,

    Well I guess I didnt frame my words right in the previous , I wanted to increment the whole string and also wanted to know the behaviour of the charecters in a string being incremented from the right to left.

    regards

    prad
      The behaviour is the same as if you increment a number, only the "numbers" are not 0-9, but a-z. The simplest way to see that is to start "counting" from "a".
      $s = "a"; for (1..702) { print $s++, "\n"; }
      prints
      a b c ... z aa ab ac ... az ba bb bc ... zz


      holli, /regexed monk/
      well have a look at the "perlop" documentaion (auto-increment operator) to read about the behaviour.

      as for the "increment the whole string" comment, you are doing. If you increment the number 4356 to 4357, you only change one 'character', but you are operating on the whole 'string'. This is exactly the same for your example, where only the last character has rolled over, which increments the last but one. If you want to increment each character in the string, you may want to do something like this.

      $sample='prad'; print join('', map { ++$_} split(//, $sample) ); print"$sample";
      but be careful, incrementing 'z' in this fasion would return 'aa'
      ---
      my name's not Keith, and I'm not reasonable.

      The behavior is not unusual. If you incremented 1111 you would get 1112, and if you incremented it ten times you would get 1121. Eleven times would give you 1122. A hundred times would give you 1211... you get the idea.

      So, given that alpha characters can also be incremented, it should come as no surprise that they're incremented right to left also; consider the right-most digit to be the least significant digit, whether it's a number or an alpha character. Thus, 'aaaa' would become 'aaab'. Increment it enough times, it it becomes 'aaba', 'aabb', 'aabc' ... 'aaca', 'aacb', 'aacc', and so on, until you finally arrive at 'zzzz'.

      If you really just wanted to increment each character by one, you might consider constructing a regexp like this:

      $string =~ s/(.)/$char = $1;++$char/eg;

      Dave

Re: string increment
by prad_intel (Monk) on Sep 13, 2005 at 09:51 UTC
    Hi All,

    Thanks for the ideas you gave which made me to make some satisfiable code ( of course to me alone and does not matches your standards )

    Here is my modiefied version which tells how many iterations is the code taking to reach from start to the letter typed.
    print"key in any 4 alpha char :"; chomp($string_name=<STDIN>); for($i='aaaa',$c=0;$i le $string_name;++$i,++$c){} print"$string_name is the $c th value starting from a\n";

    Please run this piece and let me know how do I make it generic instead of constraining it to four alpachar alone as in the above code.

    Regards

    prad

    2005-09-13 Retitled by holli, as per Monastery guidelines
    Original title: 'Modified : String Increment'

      How about some base 26 arithmetic!

      perl -e '$s="aaa";for $i (1..28) { ++$s }; print $s;'

      gives me 'abc' as its output for 28 increments. So lets consider 'abc' as a 'number':

      perl -e 'use Math::BaseCalc;$c = new Math::BaseCalc(digits => [a..z] );print $c->from_base("abc");

      The code above gives me back my 28 increments. Ain't perl fun?

      To integrate it in your code can be more difficult however because your base string doesn't have to be 'aaaaaaa'. But a little math can do the trick.

      --
      if ( 1 ) { $postman->ring() for (1..2); }
      and here's a different style solution for you.

      Note the use of use strict forcing me to decalre my variables.

      #!/perl -w use strict; print"key in your string :"; chomp( my $string_name=<STDIN>); my $low_string = "a"x length($string_name); my $count = 0; while($low_string ne $string_name) { $low_string++; $count++; } print"$string_name is the $count th value starting from $low_string\n" +;
      ---
      my name's not Keith, and I'm not reasonable.
      You just put in as many characters as you actually want, theres no limit. Any string can be incremented through the alphabet like this.

      I really have no idea what you are trying to achieve. Can you post what you want the start string to look like, and what the end string should look like? And why you are using string increment at all? It's a rather obscure way to do things IMO.

      C.

      2005-09-13
      Original title: 'Re^2: string increment'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://491497]
Approved by davido
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-10-20 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (258 votes). Check out past polls.

    Notices?