splitting text string

S

Stephen None

I am working on a way to process the status of products and I want to
use a bar code reader to make things easier. I already have the scanner
which simply reads the bar code into the keyboard port and appends a
carriage return. I was thinking of printing bar codes that would
contain information which is useless externally but useful to my app.
For instance...

Bar code: 123456789
Which would break down into:

order (first three) => 123
product (next five) => 45678
status (last digit) => 9

I would then use a nested form to feed the information in the database.

My question is... I get the string from the barcode reader as one long
string of numbers with no delimiters so how do I break that string down?
I can make sure the position of the values is the same in the barcode
when we print them but split and chunk both want a delimiter. Thanks,

stephen
 
J

Jonathan Nielsen

I am working on a way to process the status of products and I want to
use a bar code reader to make things easier. =C2=A0I already have the sca= nner
which simply reads the bar code into the keyboard port and appends a
carriage return. =C2=A0I was thinking of printing bar codes that would
contain information which is useless externally but useful to my app.
For instance...

Bar code: =C2=A0123456789
Which would break down into:

order (first three) =3D> 123
product (next five) =3D> 45678
status (last digit) =3D> 9

I would then use a nested form to feed the information in the database.

My question is... I get the string from the barcode reader as one long
string of numbers with no delimiters so how do I break that string down?
I can make sure the position of the values is the same in the barcode
when we print them but split and chunk both want a delimiter. =C2=A0Thank= s,

stephen

Use ranges as an index:

~$ irb
irb(main):001:0> barcode =3D '123456789'
=3D> "123456789"
irb(main):002:0> order =3D barcode[0..2]
=3D> "123"
irb(main):003:0> product =3D barcode[3..7]
=3D> "45678"
irb(main):004:0> status =3D barcode[8..8]
=3D> "9"

-Jonathan Nielsen
 
J

Joel VanderWerf

Jonathan said:
I am working on a way to process the status of products and I want to
use a bar code reader to make things easier. I already have the scanner
which simply reads the bar code into the keyboard port and appends a
carriage return. I was thinking of printing bar codes that would
contain information which is useless externally but useful to my app.
For instance...

Bar code: 123456789
Which would break down into:

order (first three) => 123
product (next five) => 45678
status (last digit) => 9

I would then use a nested form to feed the information in the database.

My question is... I get the string from the barcode reader as one long
string of numbers with no delimiters so how do I break that string down?
I can make sure the position of the values is the same in the barcode
when we print them but split and chunk both want a delimiter. Thanks,

stephen

Use ranges as an index:

~$ irb
irb(main):001:0> barcode = '123456789'
=> "123456789"
irb(main):002:0> order = barcode[0..2]
=> "123"
irb(main):003:0> product = barcode[3..7]
=> "45678"
irb(main):004:0> status = barcode[8..8]
=> "9"

-Jonathan Nielsen

Another idea:

$ irb -r scanf=> [123, 45678, 9]

(note that the strings are converted to integers for you)
 
B

Brian Candler

Joel said:
Jonathan said:
order (first three) => 123
stephen
irb(main):004:0> status = barcode[8..8]
=> "9"

-Jonathan Nielsen

Another idea:

$ irb -r scanf=> [123, 45678, 9]

(note that the strings are converted to integers for you)

Another:

raise "Bad barcode" unless "123456789" =~ /\A(\d{3})(\d{5})(\d)\z/
order, product, status = $1, $2, $3
 
S

Siep Korteling

Joel said:
Another idea:

$ irb -r scanf=> [123, 45678, 9]

(note that the strings are converted to integers for you)

"123456789".unpack("A3A5A1")
#=> ["123", "45678", "9"]

I don't know how to use unpack to deliver integers. Strings may better
in this case anyway, because leading zero's survive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top