[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Need some help here



There's a whole whack of ways to do these things. For example,

   #!perl -w

   # convert the input string into a list of numbers
   @ords = unpack( "C*", $ARGV[0] );
   print "@ords\n";

   # convert same string into pairs
   @pairs = ($ARGV[0] =~ /(..)/g);
   print "@pairs\n";

where this script acts on the string argument you pass in on the command line.

There is no built-in way to match even numbers, but something like

   @evennums = map { $_ % 2 ? () : $_ } ($ARGV[0] =~ /(\d)/g);

is one of about 200 ways to do it. :-)

Note: all of the expressions above will look quite different depending on 
how the initial string is presented. I happen to use the command-line. Also, 
you can use any of the global matching constructs in a while loop, and 
tackle the chunks one by one.

Arved Sandstrom

At 11:53 AM 4/30/00 +0000, Sveinbjorn Thordarson wrote:
>Hi there folks
>
>I need some help with a slight problem I have.  I need to convert
>letters [A-Za-Z] into numbers.   What is the best way to do this without
>having to type in an endless row of "if $letter = 'letter'?
>
>Also,I would like to split a designated string into pairs of characters,
>ie I want john to be put into an array as 'jo' and 'hn' and 2476 to put
>put into the array as '24' and '76'...I know these are awful newbie
>questions and all, but I'd appreciate any help!  Thanx.
>
>Sveinbjorn Thordarson
>Reykjavik, Iceland
>
>PS:  Uhm...are there any built-in functions in perl to match only even
numbers?
>
># ===== Want to unsubscribe from this list?
># ===== Send mail with body "unsubscribe" to macperl-request@macperl.org
>
>


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org