View Full Version : extracting numbers
colette
11-21-2003, 05:48 PM
Hi All,
Can anyone tell me how to extract just numbers from a cookie as strings?
I can extract the whole cookie but not just the numbers? I'm okay after that because I'm fine to convert the strings to numbers with parseInt().
Thanks in advance
Colette
:thumbsup:
liorean
11-21-2003, 06:11 PM
What is the structure of your cookies?
If you want just to transform a number into a string, just use:
String()
ex:
String(1)+String(2) = '12'
colette
11-22-2003, 04:37 PM
Hi Liorean,
My cookie is:
<script language=javascript type="text/javascript">
<!--
function SetCookie(name, value)
{
document.write("Setting cookie: "+name+"<P>");
document.write("With value: "+value+"<P>");
document.cookie = name + "=" + escape(value);
document.write("Here the company normally put their own texts to introduce the
company.");
}
// -->
</script>
</HEAD>
<BODY onLoad="SetCookie('sales', 'pan1=23,pan3=202,pan4=21,pan5=12')" >
Obviously there are bits of this that can be ignored, I just did a straight copy & paste. What I need to do is on another page (of the same site) extract the list of numbers as a bunch of strings, then I'm going to convert the strings to numbers to use the data to generate a bar graph.
Many thanks for any help
Colette
:thumbsup:
liorean
11-22-2003, 09:36 PM
First, split it at commas. Second, split each of those at equal signs. Third, make an object of it:var
aCookieValues = sCookieValue.split(/\s*,\s*/), // Make an array out of the cookies in the string
i, // Define i
j = (i = aCookieValues.length), // Store the length of the array in i and j, for looping over
oCookieValues = {}; // And make an empty object to hold the final resulting object
while(i-->0) // For each cookie
aCookieValues[i] = aCookieValues[i].split('='); // Make it a two element array containing [CookieName, CookieValue] instead of a string
// aCookieValues will now be an array of the following structure,
// based on your example:
// [
// ['pan1', '23'],
// ['pan3', '202'],
// ['pan4', '21'],
// ['pan5', '12']
// ]
while (j-->0) // For each cookie
oCookieValues[aCookieValues[j][0]] = parseFloat(aCookieValues[j][1]); // make a property in the holder object named CookieName, which contains the CookieValue
// ... parseFloat to allow decimal numbers.
// Use parseInt if you wish to only allow integers instead.
// oCookieValues will now be an object of the following structure:
// {
// pan1: 23,
// pan3: 202,
// pan4: 21,
// pan5: 12
// }
// and you will be able to access the properties of oCookieValues
// using the follwing syntax:
// oCookieValues['pan1'] // => 23
// oCookieValues['pan3'] // => 202
// oCookieValues['pan4'] // => 21
// oCookieValues['pan5'] // => 12
// Or using the following syntax:
// oCookieValues.pan1 // => 23
// oCookieValues.pan3 // => 202
// oCookieValues.pan4 // => 21
// oCookieValues.pan5 // => 12
colette
11-23-2003, 01:04 PM
Hi Liorean,
thanks very much for the reply, have a problem though, it keeps giving me an error message saying i is undefined? If you can help (again) be much appreciated.
Thanks again for your help
colette
:thumbsup:
liorean
11-23-2003, 01:48 PM
Hmm, it shouldn't be undefined.
Can you show me the code as you used it? (including the reading of the cookie)
colette
11-23-2003, 03:02 PM
Hi again,
This is what i put in and i used an alert at the end to see if the numbers showed:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
<HEAD>
<TITLE>Chart of pan sales.</TITLE>
<script language="javascript" type="text/javascript">
<!--hide me from old browsers
if(top.location == self.location){
self.location.replace("index.htm?chart.htm")
}
//end hide-->
</script>
</HEAD>
<BODY>
The sales of pans are shown in the chart below.
<script language="JavaScript">
var sCookieValue = unescape(document.cookie);
var
aCookieValues = sCookieValue.split(/\s*,\s*/);
i,
j = (i = aCookieValues.length);
oCookieValues={};
while(i-->0);
aCookieValues[i] = aCookieValues[i].split('=');
while (j-->0);
oCookieValues[aCookieValues[i][0]] = parseInt(aCookieValues[i][1]);
</script>
<script>
alert(oCookieValues);
</script>
</BODY>
</HTML>
It's probably completely wrong and stupid, been trying to get this for so long now my brains complete mush!! I'm okay with other scripts (as long as they are simple) but this is beyond me :confused:
Thanks again, and sorry if I'm beyond help
Colette
:thumbsup:
liorean
11-23-2003, 06:40 PM
Hmm, first, an error I made:
while (j-->0);
oCookieValues[aCookieValues[i][0]] = parseInt(aCookieValues[i][1]);
should have been:
while (j-->0);
oCookieValues[aCookieValues[j][0]] = parseInt(aCookieValues[j][1]);
That shouldn't make much of a difference, though. No, your error seem more likely to belong in this bit of my code:var
aCookieValues = sCookieValue.split(/\s*,\s*/),
i,
j = (i = aCookieValues.length),
oCookieValues={};which you changed to
var
aCookieValues = sCookieValue.split(/\s*,\s*/);
i,
j = (i = aCookieValues.length);
oCookieValues={};
It's the small things that makes the difference. You should correct those and try again, and then tell me if it still gives that error.
colette
11-23-2003, 07:16 PM
Hi again liorean :rolleyes: ,
This is my code exactly as it is in the notepad now:
<BODY>
The sales of pans are shown in the chart below.
<script language="JavaScript">
var sCookieValue = unescape(document.cookie);
var
aCookieValues = sCookieValue.split(/\s*,\s*/),
i,
j = (i = aCookieValues.length),
oCookieValues={};
while(i-->0);
aCookieValues[i] = aCookieValues[i].split('=');
while (j-->0);
oCookieValues[aCookieValues[j][0]] = parseInt(aCookieValues[j][1]);
</script>
<script>
alert(oCookieValues);</script>
</BODY>
</HTML>
the same thing stands, I have the alert at the end to see if the cookie numbers show and the alert that is displayed is:
[Object Object]
This can't be right is it?
There's also a different script error message now, I've taken a screendump of it and attached it so you can see exactly what it's saying.
I really do appreciate all the help you're giving, I wish I was as brill with JS as you :(
Many many thanks again
Colette
:thumbsup:
colette
11-23-2003, 07:23 PM
Hi Liorean,
I don't think the attachment went on so I'm trying again :mad:
Colette
:thumbsup:
liorean
11-23-2003, 07:30 PM
No, in fact [object Object] is exactly what should be displayed. If you want to read one of the cookies, use oCookieValue[string]; where string is the name of the cookie, instead. If you want to know what cookies exist, you can either ask whether the cookie with a specific name exists:if(typeof oCookieValues['pan1'] != 'undefined')
alert('pan1: '+oCookieValues['pan1']); or loop through the members of oCookieValues:var
aCookiesThatYouCanRead = [];
for(var i in oCookieValues)
aCookiesThatYouCanRead.push(i);
alert(aCookiesThatYouCanRead.join('\n'));
There, does that address your problems?
liorean
11-23-2003, 07:49 PM
Originally posted by colette
There's also a different script error message now, I've taken a screendump of it and attached it so you can see exactly what it's saying.
Hmm, do you think you could try the following:<BODY>
The sales of pans are shown in the chart below.
<script language="JavaScript">
var sCookieValue = unescape(document.cookie);
// 01 - Possible error source 01, Cookie Reading.
alert('01\n\nsCookieValue: ' + sCookieValue);
// Should be a string along the lines of
// 'pan1=23,pan3=202,pan4=21,pan5=12'
var
aCookieValues = sCookieValue.split(/\s*,\s*/),
i,
j = (i = aCookieValues.length),
oCookieValues={};
// Possible error source 02, Cookie splitting
alert('02\n\nsCookieValue: ' + sCookieValue+'\naCookieValues: '+aCookieValues);
// sCookieValue should be the same as last time,
// aCookieValue should be an array along the lines of
// ['pan1=23','pan3=202','pan4=21','pan5=12']
while(i-->0);
aCookieValues[i] = aCookieValues[i].split('=');
// Possible error source 03, Cookie parsing
alert('03\n\n'+aCookieValues.join(';\n'));
// Should be a list along the lines of
// pan1, 23;
// pan3, 202;
// pan4, 21;
// pan5, 12
while (j-->0);
oCookieValues[aCookieValues[j][0]] = parseInt(aCookieValues[j][1]);
</script>
</BODY>
</HTML>
I really do appreciate all the help you're giving, I wish I was as brill with JS as you :( Yah, I know the feeling (felt just the same when I first came to this list, toward JavaScript genii Alex Vincent and beetle), but that was some years back for me. Stay coding for a few years more and you'll be just as good. (I started coding JavaScript when I was twelve, using nn2 for a browser. I'm twenty now...)
colette
11-23-2003, 08:30 PM
Me again :o
I know it's a bit of a cheek cos I'm sure you've much better things to be doing but if I attach the notepad code can you show exactly where each bit should be placed init?
I'm still getting error messages, now things like expected ; and such like :(
Also those alerts you said in last reply aren't doing anything, I'm going to be removing them anyway but I just wanna make sure the cookie is being read correctly as I have to use the results to generate a bar graph with.
If you could also include a few tiny comment lines so I understand why it's doing what it's doing be good (double cheek I know )
I thank you again heartfeltedly
Colette
:thumbsup:
liorean
11-23-2003, 08:49 PM
Ah, the expected ';' error was because of my sloppyness... I would have been able to do this debugging so much faster if I had been able to test it, but alas, I can't do that without the cookie, as my suspicion is that the problem lies in the cookie reading.
I'll change the code in my previous post a bit, so in a few minutes you should have a commented version.
colette
11-23-2003, 11:02 PM
Hi Liorean,
If it helps I've attached the source for the page that has the cookie init. This I can't change because it's the code I have to work from.
I hope this helps you with your very kind helpfulness :o
Colette
:thumbsup:
whammy
11-24-2003, 04:22 AM
You just want to extract numbers as in digits?:
<script type="text/javascript">
<!--
// Convert to numeric
String.prototype.toNumeric = function()
{
return this.replace(/\D/g,'');
}
alert("123rdasdf456jdasfj7890".toNumeric());
// -->
</script>
:)
adios
11-24-2003, 09:11 AM
;)
colette
11-24-2003, 09:59 AM
Hi adios,
Thank you, thank you, thank you! You are an absolute life-saver :p !
Hope you don't think I'm cheeky but if you could do me one more favour and just comment this tiny bit of the cookie code for me so I understand it properly you will be my hero for life!
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
Thanks again
Colette
:thumbsup:
adios
11-24-2003, 07:45 PM
So...I do your little graphy-thing for you and...I'm a roll candy?
Well I never. :D
The cookie script is from here (http://www.webreference.com/js/column8/index.html). The code you posted is a parsing script; it's too boring to describe. Really.
The bar chart script is here (http://www.gerd-tentler.de/tools/graphs/). Not bad.
Fooled with it a bit. Results below. cya.
colette
11-24-2003, 08:29 PM
Hi Adios,
A girl can but try for a complete breakdown :o I think I understand enough of it anyway to explain it properly, I managed to tweak the graphs script, removing bits I didn't need and such like and it still works :thumbsup:
Anyway, I must say a big enormous thank you for saving my sanity, it's much appreciatd.
Until my next crisis
Colette
:thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.