Pages: [1]   Go Down
Author Topic: Filtering an Integer array of mixed 'characters' and numbers  (Read 437 times)
0 Members and 1 Guest are viewing this topic.
Durham UK
Offline Offline
Full Member
***
Karma: 1
Posts: 167
aka Craig Turner
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have an integer array which is a mixture of characters and integer numbers (the numbers and characters could be any value - this is just an example):

Code:
int Array1[]  = {'s',250,'w',10,500,'p',15 };

I want to separate out the numbers and characters into two arrays. One of characters, and one of the integer numbers to get result:

Code:
ArrayChars[3] ...contains... {'s','w','p'}
ArrayIntNums[4] ...contains...{250,10,500,15}

The characters are commands and the numbers are data for a command. So for example {'s', 250} means "set speed to 250"

I don't even know if it is possible and I may have to store the numbers and characters in separate arrays. I think one main problem might be that some numbers and characters are interchangeable (eg 's' is also 115 and visa-versa in decimal)

Can anyone point me in the right direction?

Many Thanks

Logged

Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49412
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't even know if it is possible
Depends. Are the odd positions in the array always letter equivalents, while the even positions are always numbers?

Quote
I may have to store the numbers and characters in separate arrays.
Makes more sense to me.

Quote
Can anyone point me in the right direction?
Thataway!
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27115
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the best you could do is call them characters if in a certain range, and numbers otherwise.
www.asciitable.com

0x41 to 0x5A are capital letters
0x61 to 0x7A are lowercase letters
everthing else is a number.
Your call if you want to accept other things as characters.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Durham UK
Offline Offline
Full Member
***
Karma: 1
Posts: 167
aka Craig Turner
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Na migo thisaway!  smiley-wink

Thanks for the input PaulS.

I'll go with separate arrays.

Cheers


Logged

Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

Durham UK
Offline Offline
Full Member
***
Karma: 1
Posts: 167
aka Craig Turner
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks CrossRoads that link's really useful - I keep it for future reference. 

It is more sensible to go for separate arrays for chars and data. Then I could use other data types as well.

cheers
Logged

Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

Pages: [1]   Go Up
Jump to: