Re: string increment
by Corion (Patriarch) on Sep 13, 2005 at 07:16 UTC
|
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. | [reply] [d/l] [select] |
Re: string increment
by gargle (Chaplain) 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); }
| [reply] [d/l] [select] |
Re: string increment
by GrandFather (Saint) on Sep 13, 2005 at 07:30 UTC
|
my $sample='prad';
++$sample for(1..10);
print "$sample";
Perl is Huffman encoded by design.
| [reply] [d/l] |
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 | [reply] |
|
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
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
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;
| [reply] [d/l] [select] |
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' | [reply] [d/l] |
|
$i='aaaa'
to
'a' x length $string
| [reply] [d/l] [select] |
|
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); }
| [reply] [d/l] [select] |
|
#!/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.
| [reply] [d/l] |
|
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'
| [reply] |