...

View Full Version : How to create a list of ascending numbers?



sallam
11-01-2006, 02:43 PM
Greetings

I often need to create lists of ascending numbers, but doing it manually takes forever to type all the digits.
so I wonder if there is a javascript to do that for me?

For example, I need to create a text file:
- containing numbers from 0820001 to 1149991
- in increments of 10
- with each number in a separate line
- in ascending order:

0820001
0820011
0820021
...etc.

I deeply appreciate any help, many thanks in advance.

ess
11-01-2006, 03:23 PM
Yes it is possible and I have just create a function in JavaScript to help you out.

Please note that you cannot use JavaScript to write to external files. If that is what you want to do, you should consider using PHP, ASP, or something else. In any case, the following function should give you an idea of how you may do this in PHP or ASP so that you can write to files.



function generateNumbers( from, to, increment )
{
var numbers = new Array();

// start generating numbers
for( var i = from; i <= to; i+= increment )
{
numbers.push( i );
}

// print numbers out.
for( var i = 0; i < numbers.length; i++ )
{
window.document.write( numbers[i] + "<br>" );
}
}


All you have to do is to call upon this function like so


generateNumbers( 0, 10, 2 );

the output, should be something like the following
0
2
4
6
8
10

Good luck and all the best
:thumbsup:

sallam
11-01-2006, 04:48 PM
thanks so much for your great help.
I've made this php file, but it gives me an error:

<?php

function generateNumbers( from, to, increment)
{
var numbers = new Array();

// start generating numbers
for( var i = from; i <= to; i+= increment )
{
numbers.push( i );
}

// print numbers out.
for( var i = 0; i < numbers.length; i++ )
{
window.document.write( numbers[i] + "<br>" );
}
}

generateNumbers( 0, 10, 2 );
?>

the error message:

Parse error: syntax error, unexpected T_STRING, expecting ')' in /home/~/number-list-creator.php on line 3

bigtiger
11-01-2006, 05:50 PM
thanks so much for your great help.
I've made this php file, but it gives me an error:

<?php

function generateNumbers( from, to, increment)
{
var numbers = new Array();

// start generating numbers
for( var i = from; i <= to; i+= increment )
{
numbers.push( i );
}

// print numbers out.
for( var i = 0; i < numbers.length; i++ )
{
window.document.write( numbers[i] + "<br>" );
}
}

generateNumbers( 0, 10, 2 );
?>

the error message:

Parse error: syntax error, unexpected T_STRING, expecting ')' in /home/~/number-list-creator.php on line 3

hi, sallam, be aware that javascript and php have syntax different, you may need change something to do that in php. Have a look at this :


function generateNumbers( $from, $to, $increment)
{
$numbers = array();

// start generating numbers
for ( $i = $from; $i <= $to; $i+= $increment )
{
numbers[] = $i;
}

// print numbers out.
for ( $i = 0; $i < count($numbers); $i++ )
{
echo $numbers[i] . '<br>';
}
}

Hope this help

P.S. As a reference, php always prefix variables with $ symbols

sallam
11-01-2006, 07:16 PM
many thanks indeed.
I'm now getting a new error. Here is what I tried:

<?php

function generateNumbers( $from, $to, $increment)
{
$numbers = array();

// start generating numbers
for ( $i = $from; $i <= $to; $i+= $increment )
{
numbers[] = $i;
}

// print numbers out.
for ( $i = 0; $i < count($numbers); $i++ )
{
echo $numbers[i] . '<br>';
}
}

generateNumbers( 0, 10, 2 );
?>
and this gives me an error message:
Parse error: syntax error, unexpected '[' in /~/number-list-creator.php on line 10
I mus be doing something wrong...

GJay
11-01-2006, 07:23 PM
missing $

sallam
11-01-2006, 07:38 PM
Actualy I did try it as javascript in an html page, and it worked!


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD></HEAD>

<BODY>
<script type="text/javascript">
function generateNumbers( from, to, increment )
{
var numbers = new Array();

// start generating numbers
for( var i = from; i <= to; i+= increment )
{
numbers.push( i );
}

// print numbers out.
for( var i = 0; i < numbers.length; i++ )
{
window.document.write( numbers[i] + "<br>" );
}
}
</script>
<script type="text/javascript">
generateNumbers( 0, 10, 2 );
</script>
</BODY>
</HTML>


Its working here, thanks to you:
http://www.gawaher.com/pages/number-list-creator.htm

Can you help me add a form to enter the: 'from', 'to' and 'increment' values? That way, I can directly type the numbers in the form, instead of editing the file everytime I want a new list...

bigtiger
11-01-2006, 08:37 PM
yes, take a look this :


<html>
<head>
<script type="text/javascript">
function generateNumbers( from, to, increment )
{
var numbers = new Array();

// start generating numbers
for( var i = from; i <= to; i+= increment )
{
numbers.push( i );
}

// print numbers out.
/*
for( var i = 0; i < numbers.length; i++ )
{
window.document.write( numbers[i] + "<br>" );
}
*/
return numbers.join('<br />');
}

function create_list()
{
var from = parseInt(document.forms[0].lst_from.value);
var to = parseInt(document.forms[0].lst_to.value);
var increment = parseInt(document.forms[0].lst_incrmt.value);

document.getElementById('list').innerHTML = generateNumbers( from, to, increment );
}
</script>
</head>
<body>
<form>
<p><span>From:</span></p>
<input type="text" name="lst_from" value="" /><br />
<p><span>To:</span></p>
<input type="text" name="lst_to" value="" /><br />
<p><span>Increment:</span></p>
<input type="text" name="lst_incrmt" value="" /><br />
<input type="button" name="builder" value="Build List" onclick="create_list()" /><br />
<hr />
<span id="list"></span>
</form>
</body>
</html>

Hope this help,

sallam
11-01-2006, 10:58 PM
thanks so much, this is beautiful. It works great!
Many thanks indeed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum