/    Sign up×
Community /Pin to ProfileBookmark

Algorithm help

Hi, I’m trying to do some algorithm but no luck :/

I have one data received that I store in variable line, first field [0] contains a unique id and [1]and[2] has some value.
Sometimes data is from same id than from other id.

I need to store this data in a table [] ,if data id existes, it replaces the old value, if no data id exists, it will add it to table and the values.

The code I done is this, but seems not running correct :/

[CODE]/*
table=[ [ ‘xxxx’, ‘-40’, ‘1233’ ],
[ ‘abcd’, ‘-60’, ‘1233’ ],
[ ‘zzzz’, ‘-130’, ‘1233’ ] ];
line = [ ‘zzzz’, ‘-130’, ‘1233’ ];
i=0;

line changes first field ‘zzzz’ it’s id from the data sender on a network
could be from same point or from other with a diferent id

*/

i=0;
z=table.length;
while(i<z) {
if(table[i][0] != line[0] ) {
i++
if(i==z){
table[i] = line
i=0;
break;
}
}
else {
table[i]=line;
i=0;
break;
}
}[/CODE]

Any help to this code do the right task like add new id’s to table, and if data from same id it replaces the old values on table.

Any help is welcome
Regards ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@WyCnetJun 15.2012 — It seems as if you are trying to find a record, and if it is not found you seem to wish to append it to your table. However if you find it you seem to want to update it.

(a) Start.

(b) Look for the record in the table and record its index.

(c) Append it or Update it at the index recorded.

(d) end.

Question: Will the table grow to numbers over 250? Because if it does you will be better off keeping a sorted table index, using a classic modified insertion sort.
Copy linkTweet thisAlerts:
@vitoracmmauthorJun 15.2012 — Hi tanks for reply.

Yes i'm trying to find a record in table, and if it is not found i wish to append it to table. However if it find it I want to update it.

The problem is that on new id data, it will be added to table, but after that every new id's will no be added, insted of that it will replace the last added id.

It seems that some variable is locked ?
Copy linkTweet thisAlerts:
@WyCnetJun 16.2012 — Your table declaration with its initial instantiation and the search function/loop cannot be tightly bound, which means setup the table with its initial values, then place the search and insert coding in a function, as much as possible on the same HTML page. Iframes have separate HTML page scopes.

&lt;script type="text/javascript"&gt;

table = [ [0,0,0]] ; // with appropriate values

function appendORupdate (thisLINE){
z=table.length; //I think this will give the size of dimension one of the table, but you should verify it.
// add the rest of the logic to implement the append or update
return ;
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@vitoracmmauthorJun 17.2012 — Tanks for all help but this is a table in nodejs

I found that I could use this table as a simple object using


var table = Object.create(null);

I replace my all algotithm with a simple table[line[0]] = line;

I really love nodejs

Best regards,

Vitor
×

Success!

Help @vitoracmm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...