Get a value and than use that value to compare - windows

I am facing a problem with a PowerShell variable.
My scenario is,
Inside a function, I declare a variable $a, than in a switch, I get a value and store this to variable $a.
Now in another switch in that function, I want to compare $a. But there $a returns null.
Sample code is given below:
function fun
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
$param
)
$Get_OldData = " " #declare variable
switch ($param){
'param_001' {
$Get_OldData = "test data returned"
}
Default {
$Get_OldData = "test data returned"
}
}
switch ($param){
'param_001' {
$New_Data = "New Data"
#problem is here, can not compare $Get-OldData returns null here
#though data is assigned
if ( $New_Data -eq $Get_OldData){
#logic goes here
}
}
Default {
$New_Data = "New Data"
}
}
}
What is the solution of this problem?

You have multiple issues with your code.
The main issue probably is that you are using $param within your switch which has not been set. Same applies to $Fetch. Another Issue is that your $New-Data variable contains a hypen which you either should replace with an underscore or surround with curly brackets like ${New-Data}.
Also, // does not introduce a comment, you have to use a hash #.

Related

Trying to update the value of global variable in Shell Script

myfunc3()
{
echo $1
}
myfunc2()
{
input=$(myfunc3 10)
echo "someting that will be used later"
}
myfunc1() {
var=$(myfunc2) #this does not update the value
# myfunc2 This updates the value
}
input="$(myfunc3 1)"
echo "The new value is: $input"
myfunc1
echo "The new value is: $input"
I have 3 functions that are interrelated. I am trying to update the value of the input variable. I am able to do it when I simply call the function myfunc2 but when I am trying to get the returned value like var=$(myfunc2) as I want to use it for further uses, then the value of the input variable is not updating.
I am very new to shell scripting and I'm not able to understand the reasoning behind it.
Is there any other way to return the value or calling the function?
Thanks in advance

laravel - access variable outside loop

my code:
$atts->each(function($row){
if($row->key == 30){
$flag = $row->value;
echo $flag;
}
});
The value of $flag will be printed to the site. However, when I try to use the variable $flag outside the loop the variable is unknown.
Can someone tell me what I am missing or what has to be done to have access to that variable?
Thanks in advance!
Regards,
Andreas
You would need to define that variable in the current scope and import that variable into your callback's scope with use.
$flag = null;
//Access flag by reference
$atts->each(function($row) use (&$flag) {
...
});
Since we're using Laravel Collections, I suggest you not use each() for this.
if ($row = $atts->where('key', 30)->first()) {
$flag = $row->value;
}
This assumes there is only one row with a key of 30 though.

How to use default value when given an empty string in powershell function?

I would like to use the default value when given an empty string. I am hoping for something more elegant then having an if statement to check if $Var is empty, and setting it to a default. Anyone know if this can be achieved? Also need to support powershell version 2.0.
Below is a snippet of what I'm trying to accomplish. Given an empty string I would like it to print "Var: DEFAULT".
$ErrorActionPreference = "Stop"
function Test-Function(
[parameter( Mandatory = $true )][string] $Var = "DEFAULT"
) {
# If Mandatory is set to $true an error is thrown
# "Cannot bind argument to parameter 'password' because it is an empty string."
# When Mandatory is set to $false, $Var is an empty string rather than "DEFAULT"
Write-Host "Var: $Var"
}
$EmptyString = ""
Test-Function -Var $EmptyString
Var is an empty string because you are explicitly passing an empty string. Empty strings are still objects, not null. Your call to Test-Function -Var $EmptyString fails to give you the output you are looking for as you are equating an empty string and null which is false in .Net. Your statement that "When Mandatory is set to $false, $Var is an empty string rather than 'DEFAULT'" is correct as you did pass something, an empty string so the assignment of the value "DEFAULT" was never called.
You could remove the Mandatory=$true in which case your "Default" value is displayed when the parameter is not passed.
function Test-Function(
[parameter( Mandatory = $false )]
[string] $Var = "DEFAULT"
){
Write-Host "Var: $Var"
}
Test-Function
This generates Var: DEFAULT as expected.
Regardless of whether the parameter is mandatory or not, if you pass an empty string the assignment to $Var = "Default" is never reached as $Var has a value of '' which while empty is actually a string.
Test-Function ''
This generates Var: Which may look like wrong but it output the empty string you told it to.
If you want to allow a default value to be assigned when the parameter is not passed use the Mandatory=$false and assign a default value as I did above. If you want to test the value that was passed and assign a default value if it was an empty string you should do that in the begin block of your function.
function Test-Function(
[parameter( Mandatory = $false )]
[string] $Var = "DEFAULT"
){
begin{if([String]::IsNullOrWhiteSpace($Var)){$Var="DEFAULT"}}
process{Write-Host "Var: $Var"}
end{}
}
Test-Function
Test-Function ''
Test-Function 'SomeValue'
This generates the following which I believe to be what you expected:
Var: DEFAULT
Var: DEFAULT
Var: SomeValue
There is no elegant way to do that. At least there is not a built-in way to do so. You can just validate that the string is not null or empty:
function Test {
Param (
[ValidateNotNullOrEmpty()]
[String]
$String = 'DEFAULT'
)
Write-Host -Object "String: $String"
}
Use this inside your function:
If (!($var)) {$var = "DEFAULT"} #basically, if $var doesn't exist, create it
ps. I've just tested it with Mandatory = $false and it works as expected, no idea why it doesn't work for you, what does it do when mandatory = $false?

how to read parameters from console to method in groovy?

I am new to groovy.I am reading values for 2 variables from console with below lines of code.
System.in.withReader {
println "Version: "
version = it.readLine()
println "Doc Type:"
Doc=it.readLine()
call getBillID(version,Doc)
}
getBillid method is as below,
def getBillID(int version,int doc)
{
allNodes.BillID.each {
theregularExpression=/\d+_\d+_\d+_\d_\d+_\d+_\d_${version}_${Doc}_\d+_\d+/
if(it != "" && it =~ theregularExpression) {
println "******" + it
}
}
}
now i want to use those variable values in my getBILLID method but i am getting error as
No signature of method: ReadXML.getBillID() is applicable for argument types: (java.lang.String, java.lang.String) values: [9, ]
where i went wrong.can any one tell me plz..
In addition to #Kalarani's answer, you could also do this:
System.in.withReader {
print "Version: "
int version = it.readLine() as int
print "Doc Type: "
int doc = it.readLine() as int
getBillID( version, doc )
}
As an aside; I would be careful with your capitalisation and variable names, ie: you have a variable called Doc with a capital letter. This is not the standard naming scheme, and you are best using all lowercase for variable names. You can see where it has got confused in the getBillID method. The parameter is called doc (all lowercase), but in the regular expression you reference ${Doc} (uppercase again).
This sort of thing is going to end up causing you a world of pain and bugs that might take you longer to find
Where is the getBillId() method defined? and what is the signature of the method? It would help understanding your problem if you could post that.

string as xpath selector

I have a function that takes the result set as element and a string as args and I want to use this string as a selector inside the function.
function abc ($results as element()*, $element as xs:string)
{
for $g in distinct-values($results//*[name() = $element]) (: $results//genre :)
let $c := //$results[? = $g] (: //$results[genre=$g] :)
}
what should be in place of '?' in variable '$c'
function abc ($results as element()*, $element as xs:string)
{
for $g in distinct-values($results//*[name() = $element]) (: $results//genre :)
let $c := //$results[? = $g] (: //$results[genre=$g] :)
}
what should be in place of '?' in
variable '$c'
It is a syntax error to write:
//$results
This question is rather vague, but it seems to me that you want to group the elements, contained in $results according to their genre (or whatever element name defined by $element -- BTW this name sucks (an element isn't a string) -- better use $elementName).
The other concerning fact is that you check the complete subtrees topped by each of the elements in $results -- this means that they may have multiple genre (or whatever) descendents.
To finalize my guessing spree, it seems to me that you want this:
function grouping-Keys ($elems as element()*, $subElementName as xs:string) as element()*
{
for $g in distinct-values($elems//*[name() = $subElementName ])
return
$elems[//*[name() = $subElementName ] = $g][1]
}
If it is known that $subElementName is a name of a child that any of the elements in $elems have, then the above should be better written as:
function grouping-Keys ($elems as element()*, $childElementName as xs:string) as element()*
{
for $g in distinct-values($elems/*[name() = $childElementName ])
return
$elems[/*[name() = $subElementName ] = $g][1]
}
Both of the above functions return one element per a (distinct value of) genre (or whatever). If it is known (guaranteed) that every element in $elems has exactly one child (descendent) named genre (or whatever), then the results of these functions are not redundant.

Resources