PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Pushing things Arrays
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Pushing things Arrays

Réponse
 
LinkBack Outils de la discussion
Vieux 06/01/2008, 01h40   #1
Wally Terrible
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Pushing things Arrays

[Note: parts of this message were removed to make it a legal post.]

I've been learning Ruby and have done a couple of the quizes on the
rubyquiz.com web site and written a couple of my own. Recently, I was
surfing and stumbled on Graham King's credit card generator <
http://www.darkcoding.net/projects/credit-card-generator/> He included
several versions including PHP, Python and Java, but not Ruby. I thought it
would be a good exercise translating it into Ruby and started working on it.
What I thought would be an exercise that would take a couple of hours has
now taken a few days, and I'm stumped.
The problem involves the arrays used to hold the prefixes for the different
cards. For some reason the program changes the original prefix so if a
prefix is called that was called before it uses the entire randomly selected
number.
--snip--
American Express
----------------
345258182650083
377822445136536
3778224451365361
3452581826500839
34525818265008392

Discover
--------
6011448520213005
60114485202130056
601144852021300560

Diners Club / Carte Blanche
---------------------------
30232254153565
30032768147008
38878817351526
--snip--

I think the part that is causing problems is in here:

def completed_number(prefix, len)
newCcnumber = []
newCcnumber = prefix
len = len

# generate digits

while newCcnumber.length < (len - 1)
ran = rand(9)
newCcnumber.push(ran).to_s
end

#reverse number
reversedCCnumber = newCcnumber.reverse

#calculate sum

sum = 0
pos = 0

while pos < (len - 1)

odd = (reversedCCnumber[pos].to_i) * 2
if odd > 9
odd -= 9
end

sum = sum + odd;

if pos != (len - 2)

sum = sum + (reversedCCnumber[pos +1]).to_i
end
pos += 2
end
#calculate check digit

checkdigit = (((sum/10) + 1) * 10 - sum) % 10

newCcnumber.push(checkdigit)

newCcnumber = newCcnumber.to_s+"\n"
end

I don't really want a full solution to this problem, since I started it as a
learning exercise. What I would like is a push in the right direction. What
am overlooking? Am I way off on this problem or is it just something simple
that I'm missing?

amexPrefixList = [ ['3', '4'],
['3', '7'] ]

discoverPrefixList = [ ['6', '0', '1', '1'] ]

dinersPrefixList = [ ['3', '0', '0'],
['3', '0', '1'],
['3', '0', '2'],
['3', '0', '3'],
['3', '6'],
['3', '8'] ]

  Réponse avec citation
Vieux 06/01/2008, 05h19   #2
Paul Stickney
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pushing things Arrays

These lines look VERY suspicious:
newCcnumber = []
newCcnumber = prefix



**hint: newCcnumber and prefix are variables which *refer to the same
object* after the last statement

  Réponse avec citation
Vieux 06/01/2008, 07h27   #3
Tiziano Merzi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pushing things Arrays

Wally Terrible wrote:
> I've been learning Ruby and have done a couple of the quizes on the


yes

newCcnumber = []
newCcnumber = prefix

now newCcnumber and prefix refer the same array

newCcnumber = []
newCcnumber.concat prefix # append the elements of prefix to
newCcnumber

newCcnumber and prefix refer to different array

tiziano
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/01/2008, 13h28   #4
Wally T Terrible
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pushing things Arrays

Thanks. That was it. I suspected I was overlooking something simple. Now
it's working as expected.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11279 seconds with 12 queries