473,669 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compare a list of values to an array ...

67 New Member
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 2716
gits
5,390 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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
7951
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' => 'aaa') , array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb') , array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc') , array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
30
3438
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 I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
8
4484
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 with weird stuff, just strings, int, bools and DataTime values) My fields values for this record, for convenience are also in an array of objects. Now I am trying to write code like the following. private void processData (object d, object a)...
5
37810
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
7958
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 original statement is this (and note, when I add line above to it, that does not work): function uniqueValues(){
4
3942
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 colors represented in the image. and an percentage of how much the color exists. Lets assume that the color of the fabric is blue, it will probably have pixels that are not #0000FF since the diferent threads in the fabric
12
29446
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 follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
3
2891
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: (OPTION STRICT MUST BE ON!!) Private Function ISArrayEquals(ByVal A As Array, ByVal B As Array) As Boolean For index As Integer = 0 To A.Length - 1 If A.GetValue(index) <B.GetValue(index) Then '<----Here is the error! Return False End If
3
10550
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 format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you want to sort by Last Name, First Name or Age, using the .Sort function
4
4235
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 value from a form $_POST; say I assign a variable
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.