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

generating a character sequence

by Rudif (Hermit)
on Aug 22, 2001 at 02:55 UTC ( #106804=perlquestion: print w/replies, xml ) Need Help??

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

Hi all

I need to generate simple sequences of $n characters, for any positive $n (within reason), starting at 'a' and wrapping back to 'a' after 'z'.
The first code line in my loop, below, generates the sequence that I need.

Question 1: Other / better / faste ways of generating this sequence, anyone?
Question 2: Why does my second code line generate something entirely different?

for my $n (25..28) { printf"%s\n",pack"C*",map{$_%26+ord'a'}0..$n-1; # ok printf"%s\n",pack"C*",map{ord'a'+$_%26}0..$n-1; # not ok } __END__ abcdefghijklmnopqrstuvwxy 0123456789111111111122222 abcdefghijklmnopqrstuvwxyz 01234567891111111111222222 abcdefghijklmnopqrstuvwxyza 012345678911111111112222220 abcdefghijklmnopqrstuvwxyzab 0123456789111111111122222201

Replies are listed 'Best First'.
Re: generating a character sequence
by I0 (Priest) on Aug 22, 2001 at 06:15 UTC

      Let me add a short explanation to that. This is a precedence problem, your statement is read as (I only show the map part here): map { ord('a'+$_%26) } 0..$n-1;Both things are added first and then passed to ord - btw, with use warnings you get a warning about 'a' not being numeric in addition ... this should have been sufficient as an explanation ;-)

      So I0's solution works nicely or you can also write map { ord('a')+$_%26 } 0..$n-1; according to the rule: 'What looks like a function, is a function!'

      -- Hofmator

Re: generating a character sequence
by little (Curate) on Aug 22, 2001 at 03:04 UTC
    printf"%s\n",pack"C*",map{ord'a'+$_%26}0..$n-1; # not ok
    then $_ is ord'a' and not one of the intended values of 0..(25..28)-1

    Have a nice day
    All decision is left to your taste
Re: generating a character sequence
by runrig (Abbot) on Aug 22, 2001 at 03:05 UTC
    I'm guessing pack is faster, but just for variety here's another option:
    my $n = 42; my $str = join '',(("a".."z") x ($n/26 +1))[0..$n-1]; print $str,"\n"; print length($str),"\n";
Re: generating a character sequence
by George_Sherston (Vicar) on Aug 22, 2001 at 03:23 UTC
    Do these have anything to offer? They do the same thing in slightly different ways. Both let you start your sequence where you like.
    $l = a; #or whatever letter you want to start with for my $n (1..28) {print $l; $l++; $l=substr($l,0,1)} $l = a; #or whatever letter you want to start with for my $n (1..28) {print $l; $l eq 'z' ? $l = 'a' : $l++ }

    George Sherston

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://106804]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-12-02 05:01 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (13 votes). Check out past polls.