Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: string increment

by prad_intel (Monk)
on Sep 13, 2005 at 09:51 UTC ( [id://491531]=note: print w/replies, xml ) Need Help??


in reply to string increment

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'

Replies are listed 'Best First'.
Re: Re: string increment
by wfsp (Abbot) on Sep 13, 2005 at 10:06 UTC
Re: Re: string increment
by gargle (Chaplain) on Sep 13, 2005 at 11:48 UTC

    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); }
Re: Re: string increment
by reasonablekeith (Deacon) on Sep 13, 2005 at 10:42 UTC
    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.
Re: Re: string increment
by castaway (Parson) on Sep 13, 2005 at 10:37 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://491531]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found