Posts Tagged ‘How to’

How do I sort a concatenated array in Javascript?

Sunday, June 13th, 2010

my code:

var company=new Array("Kestrel Moon:","BB:");
var basicPri=new Array(1165,1231);

for(var i=0;i<15;i++){
var companyTotal=company[i].concat(basicPri[?

document.write(""+companyTotal+"")

It shows on the screen:

Kestrel Moon: 1165

BB: 1231

I want to sort the array so that it goes ascending order of highest value of price so it should display it as:

BB: 1231

Kestrel Moon: 1165

A normal sort would not do it as it would sort the prices but the company names stay where they are, how do I sort both arrays so it would display what I want to display?

Thank You

doc,pdf viewer

Sunday, June 13th, 2010

Is there any free/paid software/source code which provides a viewer for doc,pdf,xls,ppt etc files?

Select-all checkboxes in a FORM_TAG

Sunday, June 13th, 2010

In a form_tag, there is a list of 10 to 15 checkboxes:

<%= check_box_tag 'vehicles[]', car.id %>

How can I select-all (put a tick in every single) checkboxes by RJS? Thanks

EDIT: Sorry I didn’t make my question clear. What I meant to ask is how to add a “Select/Un-select All” link in the same page to toggle the checkboxes.

How to approach parallel processing of messages?

Sunday, June 13th, 2010

I am redesigning the messaging system for my app to use intel threading building blocks and am stumped trying to decide between two possible approaches.

Basically, I have a sequence of message objects and for each message type, a sequence of handlers. For each message object, I apply each handler registered for that message objects type.


The sequential version would be something like this (pseudocode):

for each message in message_sequence                     <- SEQUENTIAL
    for each handler in (handler_table for message.type)
        apply handler to message                         <- SEQUENTIAL

The first approach which I am considering processes the message objects in turn (sequentially) and applies the handlers concurrently.

Pros:

  • predictable ordering of messages (ie, we are guaranteed a FIFO processing order)
  • (potentially) lower latency of processing each message

Cons:

  • more processing resources available than handlers for a single message type (bad parallelization)
  • bad use of processor cache since message objects need to be copied for each handler to use
  • large overhead for small handlers

The pseudocode of this approach would be as follows:

for each message in message_sequence                              <- SEQUENTIAL
    parallel_for each handler in (handler_table for message.type)
        apply handler to message                                  <- PARALLEL

The second approach is to process the messages in parallel and apply the handlers to each message sequentially.

Pros:

  • better use of processor cache (keeps the message object local to all handlers which will use it)
  • small handlers don’t impose as much overhead (as long as there are other handlers also to be run)
  • more messages are expected than there are handlers, so the potential for parallelism is greater

Cons:

  • Unpredictable ordering – if message A is sent before message B, they may both be processed at the same time, or B may finish processing before all of A’s handlers are finished (order is non-deterministic)

The pseudocode is as follows:

parallel_for each message in message_sequence                     <- PARALLEL
    for each handler in (handler_table for message.type)
        apply handler to message                                  <- SEQUENTIAL

The second approach has more advantages than the first, but non-deterministic ordering is a big disadvantage..

Which approach would you choose and why? Are there any other approaches I should consider (besides the obvious third approach: parallel messages and parallel handlers, which has the disadvantages of both and no real redeeming factors as far as I can tell)?

Thanks!

Stupid Question Regarding If-Else’s Simultaneous Execution in C++ or C

Sunday, June 13th, 2010

In C or C++

if ( x )
    statement1;
else
    statement 2;

for what value of x both statements will be executed??
I know we can execute if-else together like this:

if(1){
    goto ELSE;
}
else{
    ELSE:
}

Is there any way, like a value?
(Which i think is not possible. Asking because someone is arguing!)

Lowest context switch time

Sunday, June 13th, 2010

Which processor has the best context switch time ?

Chaining your own method in Ruby on Rails

Sunday, June 13th, 2010

In my Rails app, I am used to using syntax like the following in a number of places, including helpers/application_helper.rb:

def my_method(x,y)
  return x+y
end

I am also used to calling the resulting method from basically anywhere in my app using syntax like this:

my_method(2,3)

However, I’d like to be able to use syntax like like this:

class_from_my_rails_app.my_method(3)

How and where do I define my_method so I can use it like this?

I’m happy to consult the documentation, but I just don’t know what the latter style is called. What do you call it?

Many thanks,

Steven.

difference between erase and remove/remove_if algorithms?

Sunday, June 13th, 2010

What is really the difference between the algorithms remove and remove_if and the member function erase?
Does both of them result in a call to the removed objects destructor?

ASP.NET plug-in architecture, settings problem

Sunday, June 13th, 2010

I want to divide business layer (BLL) of an asp.net application into multiple components. Each component is a .NET class library which is compiled as a standalone DLL. These components should have their own configuration files. For example “MyNameSpace.Users.dll” contains classes about users of the website and there’s a password policy to check if password length is at least x characters. When webmaster edits the config file of this DLL and set x to y then component (DLL) should use new value (y) in the future and enforce passwords to be at least y characters. I want each component as a single project and compile them separaely (and not to put all projects in a solution in Visual Studio), and put the DLLs of these libraries into the “Bin” folder of my ASP.NET application.

Is it possible ?
Where should I put these config files ?

how to demonstrate that a protocol is certain with those specifications.

Sunday, June 13th, 2010

Hi every one,

we have 4 persons A, B, C and D witch want to know the averge of their salary SA SB SC SD but no one wants that the others know his salary. For that they use this protocol:

  1. A->B: [N+SA ]KB
  2. B->C:[N+SA+SB]KC
  3. C->D:[N+SA+SB+SC]KD
  4. D->A:[N+SA+SB+SC+SD]KA

where the notation [m]KY represents the message x crypted xith the public key of y

Is this protocol certain. can we trust it.
want you please give me justification.

thanks for help.