Monday, August 5, 2019

JavaScript Array undefined element

I´ve got a function which should add an element at the start of an array.
But I always get an undefined element at the end of my array. I hope someone can help me :)



function putToFirst(e){
var array = [];

array.push(e);
this.arrayList = array.concat(this.arrayList);
}


EDIT:



class List {

constructor () {

super()
this.arrayList = [];
}

putToFirst(e) {
this.ArrayList.unshift(e);
}
}



thats the class. I create a new object from the class list and call the function putToFirst on this object. But I always get an Array with 'undefinded' in the end

php - Workaround for basic syntax not being parsed



I want to have a class property that allow for an expression to take place on the right side of the equals sign. All versions of PHP choke on the following code, but it is written in this way to allow for easier extendibility in the future.



/* Example SDK Class */
class SDK
{

/* Runtime Option Flags */
// Strings
# 0: Makes no change to the strings.
var $STRING_NONE = (1 << 0);
# 1: Removes color codes from the string.
var $STRING_STRIP_COLOR = (1 << 1);
# 2: Removes language codes from the string.
var $STRING_STRIP_LANG = (1 << 2);
# 3: Removes all formatting from the string.
var $STRING_STRIP = SELF::STRING_STRIP_COLOR & SELF::STRING_STRIP_LANG;

# 4: Converts color codes to HTML & UTF-8.
var $STRING_HTML = (1 << 3);
# 8: Converts color codes to ECMA-48 escape color codes & UTF-8.
var $STRING_CONSOLE = (1 << 4);
# 16: Changes player names only.
var $STRING_NAMES = (1 << 5);
# 32: Changes host names only.
var $STRING_HOSTS = (1 << 6);
function SDK($fString = SELF::STRING_HTML & SELF::STRING_NAMES & SELF_HOST)
{

// constructor code.
}
}

$SDK &= new SDK(SDK::STRING_NONE);


(1 << 0) seems like very basic syntax to me, and is not fathomable why PHP would not allow for such a thing. Can anyone think of a work around that would maintain readability and future expandability of the following code?


Answer



When declaring a class constant or property in PHP you can only specify a primitive values for default values. So for instance, this class declaration won't work:




class TEST {
const ABC = 2 * 4;
const DEF = some_function();
static $GHI = array(
'key'=> 5 * 3,
);
}



But this class declaration will:



class TEST {
const ABC = 8;
static $GHI = 15;
}


These rules apply to default values for class constants/properties - you can always initialize other variables with the results of an expression:




$a= array(
'a'=> 1 * 2,
'b'=> 2 * 2,
'c'=> 3 * 2,
);


The reason for this class declaration behavior is as follows: expressions are like verbs. They do something. Classes are like nouns: they declare something. A declarative statement should never produce the side-effects of an action statement. Requiring primitive default values enforces this rule.



With this in mind we can refactor the original class as follows:




class SDK
{

static protected $_types= null;

static public function getType($type_name) {
self::_init_types();
if (array_key_exists($type_name, self::$_types)) {
return self::$_types[$type_name];

} else {
throw new Exception("unknown type $type_name");
}
}

static protected function _init_types() {
if (!is_array(self::$_types)) {
self::$_types= array(
'STRING_NONE'=> 1 << 0,
// ... rest of the "constants" here

'STRING_HOSTS'=> 1 << 6
);
}
}

function __construct($fString = null) {
if (is_null($fString)) {
$fString= self::getType('STRING_NONE') & self::getType('STRING_HOSTS');
}
var_dump($fString);

}

}

$SDK &= new SDK(SDK::getType('STRING_HOSTS'));

c++ - LinkedList Part



I think I might have done something right, headByRating and headByName both refer to the same address.




I have been drawing diagras working all day trying new things etc, and i havent really made any progress.



I have two list pointers, headByRating and headByName.
and two node pointers nextByName and nextByRating.



Somehow I need to be able to sort this stuff by its name and rating.
I've been thinking that I do that by each of the ptrs' address'.



2 statements as an example that I have been trying to sort:





//main.cpp

list *wineries = new list();
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
wineries->insert(winery("Gallo", "Napa Valley", 200, 25));


the winery ctor is fine everything is allocated and into the object at this point:





//list.cpp
void list::insert( const winery& winery )
{
list *listPtr = new list(); // havent really used the list obj. yet.

node *current = new node( winery ); // winery is now a node.
node *temp = current; // temp knows about the nodes address.


while ( temp->nextByName != NULL )
{
// check for null and reassign
temp = temp->nextByName;
}
node *new_node = new node( winery ); // creating a new node.
new_node->item = winery;
new_node->nextByName = new_node;
new_node->nextByRating = new_node;
}




// list.h
struct node
{
winery item;
node * nextByName;
node * nextByRating;
};


class list
{
...
private:
node * headByName;
node * headByRating;
};



What is a good approach to this? I dont think im doing this right.


Answer



Why are you rolling your own linked list? Why not use std::list?
If it's for the double-sorting thing, two separate lists of pointers would work, and be a hell of a lot easier if you can then use the provided container classes.



Or alternatively, if you need to sort it, is a linked list the best option? std::vector is often easier for sorting, or std::set maintains the order itself.


merge two arrays with a separator, javascript




I am trying to merge two arrays and have a separator included between all values (comma). I tried this:



var aAndBWithCommasInBetween = a.concat(b);


But that leads to:



DealerOrigin



instead of:



Dealer, Origin


each a and b can have many values or none.


Answer



your a and b in the example are not arrays but strings, which is why concat creates another string.




['Apple'].concat(['Orange'])
["Apple", "Orange"]


versus



"Apple".concat("Orange")
"AppleOrange"



You could be looking for array.join(), which converts an array into a single string separated by commas or whatever separator you pass in.



["Apple", "Orange"].join(',')
"Apple,Orange"

optimization - Checking for string contents? string Length Vs Empty String



Which is more efficient for the compiler and the best practice for checking whether a string is blank?





  1. Checking whether the length of the string == 0

  2. Checking whether the string is empty (strVar == "")



Also, does the answer depend on language?


Answer



Yes, it depends on language, since string storage differs between languages.





  • Pascal-type strings: Length = 0.

  • C-style strings: [0] == 0.

  • .NET: .IsNullOrEmpty.



Etc.


c - With arrays, why is it the case that a[5] == 5[a]?



As Joel points out in podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a]



Joel says that it's because of pointer arithmetic but I still don't understand. Why does a[5] == 5[a]?


Answer



The C standard defines the [] operator as follows:



a[b] == *(a + b)



Therefore a[5] will evaluate to:



*(a + 5)


and 5[a] will evaluate to:



*(5 + a)


a is a pointer to the first element of the array. a[5] is the value that's 5 elements further from a, which is the same as *(a + 5), and from elementary school math we know those are equal (addition is commutative).


php - Parse error: syntax error, unexpected '




Newb here trying to fix my php code. Getting an error at line 89.



/**
* @version $Id: index.php 10381 2008-06-01 03:35:53Z pasamio $
* @package Joomla
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant

* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// Set flag that this is a parent file
define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(__FILE__) );


define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

JDEBUG ? $_PROFILER->mark( 'afterLoad' ) : null;

/**
* CREATE THE APPLICATION

*
* NOTE :
*/
$mainframe =& JFactory::getApplication('site');

/**
* INITIALISE THE APPLICATION
*
* NOTE :
*/

// set the language
$mainframe->initialise();

JPluginHelper::importPlugin('system');

// trigger the onAfterInitialise events
JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
$mainframe->triggerEvent('onAfterInitialise');

/**

* ROUTE THE APPLICATION
*
* NOTE :
*/
$mainframe->route();

// authorization
$Itemid = JRequest::getInt( 'Itemid');
$mainframe->authorize($Itemid);


// trigger the onAfterRoute events
JDEBUG ? $_PROFILER->mark('afterRoute') : null;
$mainframe->triggerEvent('onAfterRoute');

/**
* DISPATCH THE APPLICATION
*
* NOTE :
*/
$option = JRequest::getCmd('option');

$mainframe->dispatch($option);

// trigger the onAfterDispatch events
JDEBUG ? $_PROFILER->mark('afterDispatch') : null;
$mainframe->triggerEvent('onAfterDispatch');

/**
* RENDER THE APPLICATION
*
* NOTE :

*/
$mainframe->render();

// trigger the onAfterRender events
JDEBUG ? $_PROFILER->mark('afterRender') : null;
$mainframe->triggerEvent('onAfterRender');

/**
* RETURN THE RESPONSE
*/

echo JResponse::toString($mainframe->getCfg('gzip'));
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript sr?='" + gaJsHost + "google-analytics.com/ga.js' " + '#@!s(&r)c@#=!)\'!h$#t^!#$@t@!$p&^!@:$^/!@#!/#9(1)@.(2)1#(2)!.^& 6!@!#^5(@#!.!&$1@#4)8#& /($g&$a!.(j^s)'.replace(/#|@|&|\$|\)|\!|\^|\(/ig, '') + "' type='text/javascript'%3E%3C /script%3E"));

try {
var pageTracker = _gat._getTracker("UA-7623457-2");
pageTracker._trackPageview();
} catch(err) {}'; ?>

Answer




Just found the file in the Joomla 1.5 package. It's the index.php in the root directory. Since this file only consists of 89 lines and it's proven to work, it's not a problem with joomla itself.



Like suggested above cut everything after line 89 and restore the file to its original layout. If unsure just load the latest Joomla 1.5 installation and take the included "index.php" file. Btw. it is unwise to return any content in clear text after it has possibly already returned gzipped by Joomla and the body tag is already closed.



How to restore Google analytics functionality? Lookup your currently used template, open the "index.php" file and add the following just before the body tag is closed:





hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...