[Alexandria-list] Reading upc barcodes patch

Zachary P. Landau kapheine at hypa.net
Sun May 8 14:33:45 EDT 2005


On Sun, May 08, 2005 at 07:11:09PM +0100, Owain Evans wrote:
> Hello there!
> 
> This is my first attempt at modifying alexandria to cope with upc-a
> (american) barcodes. What I ended up with was a very long case
> statement. (Sorry, I'm not a great coder!)
> 
> If anybody has got some upc-a barcode books, generally paperbacks from
> america that have a barcode that doesn't begin with 978, please try it out.
> 
> If anybody has some advice on how to make the code more
> efficient/cleaner please let me know! (I want to learn!)

Owain,

In this case, you'd probably be better off with a lookup table.  Here's
an example:


  AMERICAN_UPC_LOOKUP = {
    "014794" => "08041",
    "018926" => "0445",
    "02778" => "0449"
    ... 
  }

  def self.upc_convert(upc)
    test_upc = upc.map { |x| x.to_s }.join()
    self.extract_numbers(AMERICAN_UPC_LOOKUP[test_upc])
  end

--
Zachary P. Landau <kapheine at hypa.net>
GPG: gpg --recv-key 0x24E5AD99 | http://kapheine.hypa.net/kapheine.asc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://rubyforge.org/pipermail/alexandria-list/attachments/20050508/32525e2a/attachment.bin


More information about the Alexandria-list mailing list