Connecting Tech Pros Worldwide Help | Site Map

special array cycles

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 24th, 2006, 04:05 PM
padew
Guest
 
Posts: n/a
Default special array cycles

Array so: for every value 1st column correspond one in 2nd
2 25
25 27
27 60
4 50
27 61

be able to write this
2 25,27,60,61
4 50

this steps:
-2nd isn't in right then rewrite in 1st column with its value (25)
-the 25 is also in 2nd column, then its value 27 go near 25
.....

some particular code?

  #2  
Old August 24th, 2006, 06:55 PM
Bob Smith
Guest
 
Posts: n/a
Default Re: special array cycles

On 8/24/2006 12:10 PM, padew wrote:
Quote:
Array so: for every value 1st column correspond one in 2nd
2 25
25 27
27 60
4 50
27 61
>
be able to write this
2 25,27,60,61
4 50
>
this steps:
-2nd isn't in right then rewrite in 1st column with its value (25)
-the 25 is also in 2nd column, then its value 27 go near 25
....
>
some particular code?
There are probably simpler ways to do this, but here's one:

a=[[2,25],[25,27],[27,60],[4,50],[27,61]];

function foo (x)
{
var z=[];

for (var i = 0; i < x.length; i++)
{
var y = x[i][0];
if (y 9)
y = Math.floor (y / 10);
if (z[y] == null)
z[y] = [x[i][1]];
else
z[y].push (x[i][1]);
}

return z;
}

As you can see, I'm presuming that you meant to truncate 1st col
numbers 9 to the tens (and beyond) digits.
--
_________________________________________
Bob Smith -- bsmith@sudleydeplacespam.com

To reply to me directly, delete "despam".
  #3  
Old August 25th, 2006, 02:15 AM
padew
Guest
 
Posts: n/a
Default Re: special array cycles

I have tested (see code below);
but I have some undefinid value.
Now I have:
undefined
undefined
25,27,60,61
undefined
50

anyway the result seem ok;

undefined is my bad code (can you correct?)
but I would like to have the first values 2 and 4
2 25,27,60,61
4 50
-------------
and if I use this
x=[[2,3],[3,4],[1,10],[16,12],[1,15],[2,16],[4,18],[16,19],[1,20],[6,23],[10,25],[10,26],[19,30],[25,32]];

what I must to change in the code?
I think 9 and 10, but change with 27 e 28?
I have now 14 element [ ] in X; is so?


for tests:

function foo ()
{ var var_box=document.frm.my_box;
var var_c=0;

x=[[2,25],[25,27],[27,60],[4,50],[27,61]];

var z=[];

for (var i = 0; i < x.length; i++)
{
var y = x[i][0];
if (y 9)
y = Math.floor (y / 10);
if (z[y] == null)
z[y] = [x[i][1]];
else
z[y].push (x[i][1]);
}

/*return z;*/
for (var ii = 0; ii<z.length; ii++)
{ var_box.options[var_c++]=new Option(z[ii]);}


}


</script>

<form method="post" name="frm" action="">
<select name="my_box" size="10" style="width:200px">
</select>
<br />
<input type="button" value="ok" onclick="foo()" />
</form>
  #4  
Old August 25th, 2006, 02:15 AM
padew
Guest
 
Posts: n/a
Default Re: special array cycles

Quote:
There are probably simpler ways to do this, but here's one:
you remember where and how ?
because you are the only solution, I founded at the moment.
  #5  
Old August 25th, 2006, 10:35 PM
Bob Smith
Guest
 
Posts: n/a
Default Re: special array cycles

On 8/24/2006 10:24 PM, padew wrote:
Quote:
I have tested (see code below);
but I have some undefinid value.
Now I have:
undefined
undefined
25,27,60,61
undefined
50
>
anyway the result seem ok;
>
undefined is my bad code (can you correct?)
but I would like to have the first values 2 and 4
2 25,27,60,61
4 50
-------------
and if I use this
x=[[2,3],[3,4],[1,10],[16,12],[1,15],[2,16],[4,18],[16,19],[1,20],[6,23],[10,25],[10,26],[19,30],[25,32]];
>
what I must to change in the code?
I think 9 and 10, but change with 27 e 28?
I have now 14 element [ ] in X; is so?
>
>
for tests:
>
function foo ()
{ var var_box=document.frm.my_box;
var var_c=0;
>
x=[[2,25],[25,27],[27,60],[4,50],[27,61]];
>
var z=[];
>
for (var i = 0; i < x.length; i++)
{
var y = x[i][0];
if (y 9)
y = Math.floor (y / 10);
if (z[y] == null)
z[y] = [x[i][1]];
else
z[y].push (x[i][1]);
}
>
/*return z;*/
for (var ii = 0; ii<z.length; ii++)
{ var_box.options[var_c++]=new Option(z[ii]);}
>
>
}
>
>
</script>
>
<form method="post" name="frm" action="">
<select name="my_box" size="10" style="width:200px">
</select>
<br />
<input type="button" value="ok" onclick="foo()" />
</form>
Perhaps I'm confused. If we start with

[[2,25], [25,27], [27,60], [4,50], [27,61]]

what should the result be?

Perhaps you want it to be

[[2, [25,27,60,61]], [4, [50]]]

or is it something else?

--
_________________________________________
Bob Smith -- bsmith@sudleydeplacespam.com

To reply to me directly, delete "despam".
  #6  
Old August 26th, 2006, 09:25 AM
padew
Guest
 
Posts: n/a
Default Re: special array cycles

Il Fri, 25 Aug 2006 22:42:10 GMT, Bob Smith ha scritto:
Quote:
On 8/24/2006 10:24 PM, padew wrote:
Quote:
>I have tested (see code below);
>but I have some undefinid value.
>Now I have:
>undefined
>undefined
>25,27,60,61
>undefined
>50
>>
>anyway the result seem ok;
>>
>undefined is my bad code (can you correct?)
>but I would like to have the first values 2 and 4
>2 25,27,60,61
>4 50
>-------------
>and if I use this
>x=[[2,3],[3,4],[1,10],[16,12],[1,15],[2,16],[4,18],[16,19],[1,20],[6,23],[10,25],[10,26],[19,30],[25,32]];
>>
>what I must to change in the code?
>I think 9 and 10, but change with 27 e 28?
>I have now 14 element [ ] in X; is so?
>>
>>
>for tests:
>>
>function foo ()
>{ var var_box=document.frm.my_box;
> var var_c=0;
>>
>x=[[2,25],[25,27],[27,60],[4,50],[27,61]];
>>
>var z=[];
>>
> for (var i = 0; i < x.length; i++)
> {
> var y = x[i][0];
> if (y 9)
> y = Math.floor (y / 10);
> if (z[y] == null)
> z[y] = [x[i][1]];
> else
> z[y].push (x[i][1]);
> }
>>
> /*return z;*/
> for (var ii = 0; ii<z.length; ii++)
> { var_box.options[var_c++]=new Option(z[ii]);}
>>
>>
>}
>>
>>
></script>
>>
><form method="post" name="frm" action="">
><select name="my_box" size="10" style="width:200px">
></select>
><br />
><input type="button" value="ok" onclick="foo()" />
></form>
>
Perhaps I'm confused. If we start with
>
[[2,25], [25,27], [27,60], [4,50], [27,61]]
>
what should the result be?
>
Perhaps you want it to be
>
[[2, [25,27,60,61]], [4, [50]]]
yes is so;
  #7  
Old August 26th, 2006, 07:35 PM
Bob Smith
Guest
 
Posts: n/a
Default Re: special array cycles

On 8/26/2006 5:32 AM, padew wrote:
Quote:
Il Fri, 25 Aug 2006 22:42:10 GMT, Bob Smith ha scritto:
>
Quote:
>On 8/24/2006 10:24 PM, padew wrote:
Quote:
>>I have tested (see code below);
>>but I have some undefinid value.
>>Now I have:
>>undefined
>>undefined
>>25,27,60,61
>>undefined
>>50
>>>
>>anyway the result seem ok;
>>>
>>undefined is my bad code (can you correct?)
>>but I would like to have the first values 2 and 4
>>2 25,27,60,61
>>4 50
>>-------------
>>and if I use this
>>x=[[2,3],[3,4],[1,10],[16,12],[1,15],[2,16],[4,18],[16,19],[1,20],[6,23],[10,25],[10,26],[19,30],[25,32]];
>>>
>>what I must to change in the code?
>>I think 9 and 10, but change with 27 e 28?
>>I have now 14 element [ ] in X; is so?
>>>
>>>
>>for tests:
>>>
>>function foo ()
>>{ var var_box=document.frm.my_box;
>> var var_c=0;
>>>
>>x=[[2,25],[25,27],[27,60],[4,50],[27,61]];
>>>
>>var z=[];
>>>
>> for (var i = 0; i < x.length; i++)
>> {
>> var y = x[i][0];
>> if (y 9)
>> y = Math.floor (y / 10);
>> if (z[y] == null)
>> z[y] = [x[i][1]];
>> else
>> z[y].push (x[i][1]);
>> }
>>>
>> /*return z;*/
>> for (var ii = 0; ii<z.length; ii++)
>> { var_box.options[var_c++]=new Option(z[ii]);}
>>>
>>>
>>}
>>>
>>>
>></script>
>>>
>><form method="post" name="frm" action="">
>><select name="my_box" size="10" style="width:200px">
>></select>
>><br />
>><input type="button" value="ok" onclick="foo()" />
>></form>
>Perhaps I'm confused. If we start with
>>
>[[2,25], [25,27], [27,60], [4,50], [27,61]]
>>
>what should the result be?
>>
>Perhaps you want it to be
>>
>[[2, [25,27,60,61]], [4, [50]]]
>
yes is so;
Try this instead

function foo (x)
{
var z={};

for (var i = 0; i < x.length; i++)
{
var y = x[i][0];
if (y 9)
y = Math.floor (y / 10);
if (y in z)
z[y].push (x[i][1]);
else
z[y] = [x[i][1]];
}

return z;
}

which actually returns

{2:[25,27,60,61], 4:[50]}

which should be easier to use.
--
_________________________________________
Bob Smith -- bsmith@sudleydeplacespam.com

To reply to me directly, delete "despam".
  #8  
Old August 26th, 2006, 10:15 PM
padew
Guest
 
Posts: n/a
Default Re: special array cycles

now for test I use this, and see the result on popup;
but I not see similar that your result {2:[25,27,60,61], 4:[50]};
I not see 2 and 4;
for use with alert I chenge x (now inner function)
and z={}; in z=[];

can you write the code that you use for se the results?

my modified
function foo()
{

x=[[2,25],[25,27],[27,60],[4,50],[27,61]];


var z=[];

for (var i = 0; i < x.length; i++)
{
var y = x[i][0];
if (y 9)
{y = Math.floor (y / 10);}
if (y in z)
{z[y].push (x[i][1]);}
else
{z[y] = [x[i][1]];}
}

return alert(z);
}
  #9  
Old August 28th, 2006, 03:15 PM
Bob Smith
Guest
 
Posts: n/a
Default Re: special array cycles

On 8/26/2006 6:24 PM, padew wrote:
Quote:
now for test I use this, and see the result on popup;
but I not see similar that your result {2:[25,27,60,61], 4:[50]};
I not see 2 and 4;
for use with alert I chenge x (now inner function)
and z={}; in z=[];
>
can you write the code that you use for se the results?
>
my modified
function foo()
{
>
x=[[2,25],[25,27],[27,60],[4,50],[27,61]];
>
>
var z=[];
>
for (var i = 0; i < x.length; i++)
{
var y = x[i][0];
if (y 9)
{y = Math.floor (y / 10);}
if (y in z)
{z[y].push (x[i][1]);}
else
{z[y] = [x[i][1]];}
}
>
return alert(z);
}
Go back to using z={}; and then look at return z.toSource;

--
_________________________________________
Bob Smith -- bsmith@sudleydeplacespam.com

To reply to me directly, delete "despam".
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.