473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

compare a list of values to an array ...

67
Hi gits

thanks for your reply. I am sorry about the link, I didn't think that way. I will be more careful from now.

The function as it stands now, is still giving me issues, and I need a way to reload a given array. I was wondering if you could help.

Here is the situation, the image below is of two examples of a given array, say array_1, when I call a function.



I need a way to reload this array, on function call, in other words, I need to delete unwanted entries.

Here is another array (say array_2) that I have created for that purpose, called with the same function.



The idea is, if array_1 is represented as ex. 1 while calling the function, array_1 should become -

'0'=>"41~0"
'1'=>"43~0"
'2'=>"45~0"

and if array_1 is represented as ex. 2 while calling the function, array_1 should become -

'0'=>"41~0"
'1'=>"43~2"
'2'=>"45~2"

ie, keep whatever is after the ~ or replace by 0 in case there is none, match the first part of value, and delete the rest where the first part is not a part of array_2. Given that, the values of array_1 and array_2 may or maynot be in the same order. Or, the values of array_2 can be hidden without any order in array_1, or may be totally absent, as in ex. 1

Do you have anything to propose me here?
Thanks again.
Dec 27 '07 #1
7 2697
gits
5,390 Expert Mod 4TB
hi ...

do you mean something like this:

Expand|Select|Wrap|Line Numbers
  1. var array_1 = [ '11~0', '56~0', '57~2', '58~1' ];
  2. var look_up = { 0: 41, 1: 43, 2: 45 };
  3.  
  4. function lookup_list(arr, map) {
  5.     var a = [];
  6.  
  7.     for (var i in map) {
  8.         var temp = arr[i].match(/[^\~]+$/);
  9.         a.push(map[i] + '~' + temp);
  10.     }
  11.  
  12.     return a;
  13. }
  14.  
  15. var b = lookup_list(array_1, look_up);
  16.  
kind regards
Dec 27 '07 #2
kigoobe
67
Hi gits

Honestly speaking my javascript is not so develop to understand what you wrote, but I will try with this example now, will let you know then ... thanks :)
Dec 27 '07 #3
gits
5,390 Expert Mod 4TB
Hi gits

Honestly speaking my javascript is not so develop to understand what you wrote, but I will try with this example now, will let you know then ... thanks :)
ok ... let me comment it a bit:

Expand|Select|Wrap|Line Numbers
  1. // one of your arrays
  2. var array_1 = [ '11~0', '56~0', '57~2', '58~1' ];
  3.  
  4. // instead of an array we use a hashmap here (js-object)
  5. // to iterate and store the values to add - represents your
  6. // second array
  7. var look_up = { 0: 41, 1: 43, 2: 45 };
  8.  
  9. // declare a function that compares array_1 and the map
  10. function lookup_list(arr, map) {
  11.     // literally declare an array ... always prefer this instead
  12.     // of using new Array
  13.     var a = [];
  14.  
  15.     // iterate through the map - i are the keys
  16.     for (var i in map) {
  17.         // use a regExp to match from the end until
  18.         // a '~' is found
  19.         var temp = arr[i].match(/[^\~]+$/);
  20.  
  21.         // add the value to our new array a
  22.         a.push(map[i] + '~' + temp);
  23.     }
  24.  
  25.     // return the new array
  26.     return a;
  27. }
  28.  
  29. // b gets the new array from our function assigned
  30. var b = lookup_list(array_1, look_up);
  31.  
kind regards
Dec 27 '07 #4
kigoobe
67
Hi Gits

Must tell, you are a real genious ... :)

Yeah, it's working as it should ... I'm really surprized the way you manged to do that with such a small function. I thought it would be much longer. And thanks for your explanation. Also, I didn't know it's better to declare a js array with [] instead of doing that with new. Something new I learned :)

I think the heart of that full code is this line
Expand|Select|Wrap|Line Numbers
  1. var temp = arr[i].match(/[^\~]+$/);
It's highly complex, conceptually, I must admit.
Thanks again genious :)
Dec 27 '07 #5
gits
5,390 Expert Mod 4TB
we have to do a little adaption ... since it could be that you would have a key in the list that wouldn't have a corresponding key in the array:

Expand|Select|Wrap|Line Numbers
  1. var array_1 = [ '11~0', '56~0', '57~2', '58~1' ];
  2. var look_up = { 0: 41, 1: 43, 2: 45 };
  3.  
  4. function lookup_list(arr, map) {
  5.     var a = [];
  6.  
  7.     for (var i in map) {
  8.         if (typeof arr[i] != 'undefined') {
  9.             var temp = arr[i].match(/[^\~]+$/);
  10.             a.push(map[i] + '~' + temp);
  11.         }
  12.     }
  13.  
  14.     return a;
  15. }
  16.  
  17. var b = lookup_list(array_1, look_up);
  18.  
next thing would be: is there always a '~' followed by a value in your array?

kind regards
Dec 27 '07 #6
kigoobe
67
Thanks for the update gits. Yes, actually that array_1 is a id~qnty pair (first id of the item to be selected, and then the quantity taken, starting from zero), so there's always the ~ sign, to seperate the two.

Idea is, user chooses a product for a particular category, choose number of that product, and then move to the next category. Problem that I was facing, was that when the user was moving to the next category, I was calling the same array, and it was already populated from before. So, I had to get rid of previous elements, in case the ids correspond to a different category, or keep it if it's from the same category.

OK, I'm updating my code.
Thanks again. :)
Dec 27 '07 #7
gits
5,390 Expert Mod 4TB
ok ... so the snippet should do the job well now ;) ... post back in case you encounter problems with it ...

kind regards
Dec 27 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
8
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
21
by: vadymus | last post by:
Hello, I need to compare 2 id names but not their values. For example: document.getElementById(i) == document.getElementById(j) these elements above seem to give error or do not work. My...
4
by: Jmc | last post by:
Hi Need some advice on how to get all colors in an image. I wish to have an input file, in my case it will be a scanned piece of fabric. I would like to first of all gen an array with all the...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
3
by: =?Utf-8?B?UmljY2FyZG8=?= | last post by:
What is the best method to compare 2 array to knw if each element is equal? Now, i have to compare the datarow.itemarray of 2 datarows wich is equals in structure. I tried to write this routine:...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
4
by: toadstool | last post by:
I have an array containing the various products $inventory = array(); $inventory = "whatever1"; etc I have an array containing various values $quantity = array(100,200,300); I call in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.