Converting a numerical string into an array
=> ["08", "06", "29"]
DZone Snippets > jrobertson > array
12150 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
James Robertson http://www.r0bertson.co.uk
class PasswordLookup def initialize() chars = (0..9).to_a + Array.new(7) + ('A'..'Z').to_a + Array.new(6) + ('a'..'z').to_a @chars = (0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a @doc = Document.new() root = Element.new('codes') @doc.add_element(root) chars.each do |char| node = Element.new('code') if not char.nil? node.attributes['index'] = char node.attributes['value'] = get_random_chars(2) end root.add_element(node) end puts root end def save(filepath) file = File.new(filepath,'w') file.puts @doc file.close end def get_random_chars(vword_size) newpass = Array.new(rand(vword_size) + 1, '').collect{@chars[rand(@chars.size)]}.join # return the encryption providing it doesn't already exist in the lookup table. if not /value=\'#{newpass}\'/.match @doc.root.elements.to_a.to_s return newpass else return get_random_chars(vword_size) end end private :get_random_chars end
<codes> <code value='4h' index='a'/><code value='B' index='b'/><code value='m' index='c'/> <code value='qf' index='d'/> </codes>
var t; var m_doc; function loadXml() { url = 'http://rorbuilder.info/pl/codes'; m_doc = XML.load(url); } function getCode(val,i) { pos = val.charCodeAt(i) - 48; node = m_doc.documentElement.childNodes[pos] return node.getAttribute('value'); } function timed_update(keyCode, val) { if (val.length > 0 && ((keyCode > 40) || (keyCode == 8)) ) { clearTimeout(t); t = setTimeout("revealCode('" + val + "')", 1000); } else { o = document.getElementById('out1'); if (val.length <= 0 && o.value.length > 0) { o.value = ''; } } } function revealCode(val) { var iEnd = val.length; var newcode = ''; for (i=0;i<iEnd;i++) { var codex = getCode(val,i); newcode += codex; } update(newcode); } function update(val){ o = document.getElementById('out1'); o.value = val; /*var o_copied = document.getElementById('out1').createTextRange(); o_copied.exeCommand("Copy");*/ }
<body onload="loadXml()"> <h1>Password lookup</h1> <dl> <dt><label for="in1">Enter password:</label></dt> <dd><input type="text" name="in1" id="in1" value="" onkeyup="timed_update(event.keyCode, this.value)" /></dd> <dt><label for="out1">Generated password</label></dt> <dd><input type="text" name="out1" id="out1" value=""/></dd> <dd><input type="button" name="clear1" id="clear1" onclick="clearPassword()" value="clear"/></dd> </dl> <p>see also: <a href="codes.xml" title="password code lookup table">codes.xml</a></p> </body>
y = 'testing'.scan(/./) y.each do |c| puts c end