Friday, 3 January 2020

javascript - Remove value from associative array











I am having an array, from which I want to remove a value.



Consider this as an array



[ 'utils': [ 'util1', 'util2' ] ]




Now I just want to remove util2. How can I do that, I tried using delete but it didn't work.



Can anyone help me?


Answer



Use the splice method:



var object = { 'utils': [ 'util1', 'util2' ] }

object.utils.splice(1, 1);



If you don't know the actual position of the array element, you'd need to iterate over the array and splice the element from there. Try the following method:



for (var i = object.utils.length; i--;) {
var index = object.utils.indexOf('util2');

if (index === -1) break;

if (i === index) {
object.utils.splice(i, 1); break;

}
}


Update: techfoobar's answer seems to be more idiomatic than mine. Consider using his instead.


Why do python list with a single entry give the 0th element for -1 index and 0 index?




On creating a list in python with just one element, when I try to get the -1 index element instead of getting list index out range error it is giving the 0th element.



some = ['something']
print(some[0]) # prints something
print(some[-1]) # prints something


Here is a link to the same:
https://repl.it/@hearsid/SuperiorProudShoutcast



enter image description here


Answer



A negative index means nth from the right. The zero-index of a one-element list is the same as the negative one-index of a one-element list because the zeroeth element is the same as the first element from the right.


javascript - Abort Ajax requests using jQuery

As many people on the thread have noted, just because the request is aborted on the client-side, the server will still process the request. This creates unnecessary load on the server because it's doing work that we've quit listening to on the front-end.


The problem I was trying to solve (that others may run in to as well) is that when the user entered information in an input field, I wanted to fire off a request for a Google Instant type of feel.


To avoid firing unnecessary requests and to maintain the snappiness of the front-end, I did the following:


var xhrQueue = [];
var xhrCount = 0;
$('#search_q').keyup(function(){
xhrQueue.push(xhrCount);
setTimeout(function(){
xhrCount = ++xhrCount;
if (xhrCount === xhrQueue.length) {
// Fire Your XHR //
}
}, 150);
});

This will essentially send one request every 150ms (a variable that you can customize for your own needs). If you're having trouble understanding what exactly is happening here, log xhrCount and xhrQueue to the console just before the if block.

How to parse HTML table using PHP?











I need to fetch the second column of the given HTML table using PHP. How can I do it?



References:



Table to be parsed: http://bit.ly/Ak2xay




HTML code of this table: http://bit.ly/ACdLMn


Answer



For tidy HTML codes, one of the parsing approach can be DOM. DOM divides your HTML code into objects and then allows you to call the desired object and its values/tag name etc.



The official documentation of PHP HTML DOM parsing is available at http://php.net/manual/en/book.dom.php



For finding the values of second coloumn for the given table following DOM implementation can be done:



$data = file_get_contents('http://mytemporalbucket.s3.amazonaws.com/code.txt');


$dom = new domDocument;

@$dom->loadHTML($data);
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('table');

$rows = $tables->item(1)->getElementsByTagName('tr');

foreach ($rows as $row) {

$cols = $row->getElementsByTagName('td');
echo $cols[2];
}

?>


Reference: Customized the code provided at How to parse this table and extract data from it? to match this question's demand.


php - How to turn off magic quotes on shared hosting?



I want to turn off PHP's magic quotes. I don't have access to php.ini.




When I tried to add php_flag magic_quotes_gpc off to my .htaccess file, I get a 500 internal server error. This is what my .htaccess file looks like:



AddType x-mapp-php5 .php
php_flag magic_quotes_gpc off


Then I tried to use ini_set('magic_quotes_gpc', 'O'), but that had no effect.



How do I turn magic quotes off?


Answer




As per the manual you can often install a custom php.ini on shared hosting, where mod_php isn't used and the php_value directive thus leads to an error. For suexec/FastCGI setups it is quite common to have a per-webspace php.ini in any case.



--



I don't think O (uppercase letter o) is a valid value to set an ini flag. You need to use a true/false, 1/0, or "on"/"off" value.



ini_set( 'magic_quotes_gpc', 0 );   // doesn't work


EDIT




After checking the list of ini settings, I see that magic_quotes_gpc is a PHP_INI_PERDIR setting (after 4.2.3), which means you can't change it with ini_set() (only PHP_INI_ALL settings can be changed with ini_set())



What this means is you have to use an .htaccess file to do this - OR - implement a script to reverse the effects of magic quotes. Something like this



if ( in_array( strtolower( ini_get( 'magic_quotes_gpc' ) ), array( '1', 'on' ) ) )
{
$_POST = array_map( 'stripslashes', $_POST );
$_GET = array_map( 'stripslashes', $_GET );
$_COOKIE = array_map( 'stripslashes', $_COOKIE );

}

How to remove duplicates from Python list and keep order?




Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this:



from sets import Set

[...]
myHash = Set(myList)


but I don't know how to retrieve the list members from the hash in alphabetical order.



I'm not married to the hash, so any way to accomplish this will work. Also, performance is not an issue, so I'd prefer a solution that is expressed in code clearly to a fast but more opaque one.


Answer



A list can be sorted and deduplicated using built-in functions:




myList = sorted(set(myList))



  • set is a built-in function for Python >= 2.3

  • sorted is a built-in function for Python >= 2.4


Thursday, 2 January 2020

Alternative for println in Scala



I am required to print huge amounts of data ( in the order of a few 100 MBs) on the console. Using println for this is failing miserably on IntelliJ.
Is there any alternative like console.log which can handle and display this data without lagging and slowing down?



Thanks in advance!


Answer




You can buffer, and perhaps bypass character encoding. It's also worth looking at the IntelliJ settings, particularly if you don't see this problem when running from the commandline - perhaps IntelliJ is offering some functionality in its console (e.g. highlight errors, link stack traces to line numbers) that involves scanning every line. You might also worry about word wrapping in your console



(That said, I'm not sure how you expect to understand anything from 100MB of printing - if it's something you need to look at an overview of to "see the pattern", try making code see the pattern the same way you do)


php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...