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

Re: [MacPerl] Need some help here



On Sun, 30 Apr 2000 11:53:53 +0000, Sveinbjorn Thordarson wrote:

>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'?

That must be 'eq', not "=". You'd do an assignment, and the if statement
would always succeed as the RHS of the assignment is true.

Now, back to you question: it depends on the range of characters, and on
the numbers.

If you want at most 10 consecutive numbers to be converted to a digit,
you can use tr///:

	tr/a-j/0-9/;

This would turn "abcda' into "01230".

If you want to do something related to base64 decoding, all numbers
below 256, you can use something like:

	tr/A-Za-zA-Z0-9+\//\000-\037/;

after which you can get at the individual number by doing ord on he
character.

Finally, the most generic approach is using a hash:

	%number = ( a => 1, b => 2);
	$_ = 'abba';
	@numbers = map { $number{$_} } split //;
	print "@numbers\n";  # => 1 2 2 1

>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.

I don't really uderstand this question, but I guess that the hash
approach would be appropriate.

-- 
	Bart.

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