I have a strange problem, i have a very simple function.
This is my powershell code
$exclude = #()
function GetOracleDb {
param([string]$servername)
$exclude += $servername
}
GetOracleDb "Myserver2"
$exclude
Why my $exclude array is empty?
Thanks for your help
change it like this:
$global:exclude += $servername
The scope of $exclude inside a function is different from this one outside.
Related
I'm trying to pass a JSON by reference and then add something to it. For example, I want to get the same thing as:
$json.commontokens.search.tokens.token += $newItem
I keep the keys following $json in a list of strings in $jsonList (since the keys are unknown and can change) to build $json.commontokens.search.tokens.token:
$jPath = $json
foreach($item in $jsonList){
$jPath = $jPath.$item
}
As a result, $jpath += $newItem doesn't keep the change made to $json (but works for $jpath). So I thought I needed to pass by reference to keep the change. Something like
function adder{
param(
[ref]$j
$stringList
)
if ($stringList -ne $null) {
$first = $stringList.first.value
$stringList.removefirst()
adder ([ref]$j.value.$first) $stringList
} else {
return $j
}
}
$jpath = adder ([ref]$json) $jsonList
$jpath.value += $newItem
Again, this works for $jpath, but nothing happens to $json. When I try $jpath.value[70], I get the expected answer, but $json[70] doesn't exist. In fact, I tried it without the function:
$jpath = [ref]$json.commontokens.search.tokens.token
$jpath.value += $newItem
But it still doesn't work. Am I doing the referencing wrong? Or does it have something to do with the keys?
So, I don't really understand it, but I managed to get it to work.
In adder, I changed the recursive line from
([ref]$j.value.$first)
to
([ref]$($jvalue.value.$first))
I also added a return to make it more obvious I was doing recursion.
For some reason, I still couldn't do $jpath.value += $newItem; However by removing $token from $jpath, $jpath.value.token += $newItem works.
So instead of adding all the strings from stringlist, I skipped the last one.
Adder was changed to skip the last string:
($stringList -ne $null)
was changed to
($stringList-ne $stringList.list.last)
where $stringlist is actually just a node now (I should probably change the name). So the final code is $jpath.value.($stringList.Last.value) += $newItem.
I guess I just can't straight up modify $jpath.value? Or something?
how can I count the
mysql_fetch_array($res)
because when i am puting it like this it will playing an infinite loop:
$count= mysql_fetch_array($res);
While($count)
{
}
Please tell me a method to solve this query via variable.
If you want to get the number of rows in the results, you can simply use
$count = mysql_num_rows($res);
If you want to count them in a loop, you need to call mysql_fetch_array() each time through the loop, and increment a counter inside the loop.
$count = 0;
while ($row = mysql_fetch_array($res)) {
$count++;
// do something with $row
}
The code below works for a string value but not when I try to access the variable directly.
The data being accessed is a table at http://webrates.truefx.com/rates/connect.html?f=html
My code strips it of tags and put it in an array $row0
And puts it in a function. But I can't get it out. The function is simplified for this question. I intend to concatenate some of the variables inside the function once I find out what I'm doing wrong.
$row0 = array();
include "scrape/simple_html_dom.php";
$url = "http://webrates.truefx.com/rates/connect.html?f=html";
$html = new simple_html_dom();
$html->load_file($url);
foreach ($html->find('tr') as $i => $row) {
foreach ($row->find('td') as $j => $col) {
$row0[$i][$j]= strip_tags($col);
}
}
myArray($row0); //table stripped of tags
function myArray($arr) {
$a = 'hello'; //$arr[0][0]; HELLO will come out but not the variable
$b = $arr[1][0];
$r[0] = $a;
$r[1] = $b;
//echo $r[1]; If the //'s are removed one can see the proper value here but not outside the function.
return $r;
}
$arrayToEcho = myArray($arr);
echo $arrayToEcho[0]; // will echo "first"
I have tried all the suggestions from here:
http://stackoverflow.com/questions/3451906/multiple-returns-from-function
http://stackoverflow.com/questions/5692568/php-function-return-array
Suggestion appreciated please and more info available if required. Thank you very much for viewing.
You need to get the innertext of $col in your loop. Like this:
$row0[$i][$j]= $col->innertext;
The next thing is:
myArray($row0);
This call will correctly return the parsed array; try echoing it and you'll see. But when you do this:
$arrayToEcho = myArray($arr);
...you're referencing to $arr which is a local variable (a parameter, actually) inside your function myArr. So what you probably meant was this:
$arrayToEcho = myArray($row0);
Hope this helps!
UPDATE
Look, I show you what happens when you call a function:
I want use mysql_data_seek with PDO from google search I found that it should looks like this:
$row0 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 0);
however it's not work, what I do wrong?
this is my code:
$query = "SELECT name,age FROM users";
$q = $db->prepare($query);
$q->execute();
$q->setFetchMode(PDO::FETCH_ASSOC);
$arrayData = $q->fetchAll();
foreach ($arrayData as $row){
echo $row['name'] ." ";
echo $row['age'] ."<br>";
}
$result = $q->fetch(PDO::FETCH_OBJ,PDO::FETCH_ORI_ABS,4);
var_dump($result);
I just want get the 5th row in object form from the last run query. I don't want run this query again (as some guys told me) I just want the results from sql buffer.
the var_dump result is: bool(false)
any ideas?
EDIT:
thanks for your answers and sorry but maybe I don't explain myself as well. I like the trick with JSON but the point is that the 5th row is example. I just want use the result of the query from the buffer with PDO exactly as I did it with mysql_data_seek in regular mysql (change the cursor). is it possible? I like all the tricks but that not what I look for.
the PDO 'cursor' default is PDO::CURSOR_FWDONLY that means that cursor can't back to zero like it happens with mysql_data_seek to allow cursor back to zero it necessary define use 'scrollable cursor'
example:
$db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
before use it like this:
$row0 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 0);
$result = $arrayData[4];
is all you need.
If you want the 5th row result you can do like this:
$result = json_decode(json_encode($arrayData[4]), FALSE);
var_dump($result);
or something like this:
$object = new stdClass();
foreach ($array as $key => $value)
{
$object->$key = $value;
}
Just curious! why do you need the object form?
EDIT (this will give the object form of the 5th row):
$index = 0;
$fifthRow = new stdClass();
while($row = $q->fetch())
{
if($index++==4)
$fifthRow = json_decode(json_encode($row), FALSE);
}
You could do it like this:
$c = 1;
$saved=null;
while($row = $q->fetch()){
if($c==4){
$saved = clone $row;
};
$c++;
somethingelse;
}
$saved will then contain the 4th element as an object with almost no extra overhead calculations.
I have to extract data from this link: http://bit.ly/l1rF5x
What I want to do is that I want to extract all p tags which comes under the <a> tag having attribute rel="bookmark". My only requirement is that only <p> tags which comes under this heading should be parsed, and remaining should be left as it is. Like for example in this page which I have given you, all <p> tags which comes under heading "IIFT question paper 2006", should be parsed.
help please.
You can try using the following :
$(function(){
var results= '';
$('a[rel="bookmark"] p').each(function(i,e){
results += $(e).html() + "\n";
});
alert(results);
});
Variable results will be alerted with the required content.
Example : http://jsfiddle.net/eGmWw/1/
Since you haven't provided any information about the language / environment you want to use to extract this information, I've gone ahead and hacked something together with jQuery.
(Updated) You can see it in action here: JS Fiddle.
If you wanted to use PHP, I recommend simplehtmldom
Here is an example using simplehtmldom:
$url = 'http://school-listing.mba4india.com/page/7/';
$html = file_get_html($url);
$data = array();
// Find all anchors with the desired rel attribute
foreach ($html->find('a[rel="bookmark"]') as $a) {
$h4 = $a->parent(); // Get the anchors parent (in this case an h4)
// We're assuming the next sibling is a p tag here - should test for this here
$p = $h4->next_sibling();
$content = '';
// Iterate over all following p tags, until we run out of siblings or find one
// that isn't a p tag
while ($p) {
$content .= (string) $p;
if ($p->next_sibling() && $p->next_sibling()->tag == 'p') {
$p = $p->next_sibling();
} else {
break;
}
}
$data[] = array('h4' => $h4, 'content' => $content);
}
$br = '<br/>';
foreach ($data as $datum) {
echo $datum['h4'] . $br . $datum['content'];
echo $br.$br;
}
Refer to Simplehtmldom Documentation for more!