When I go to the finder and select "Examples" then I can't figure out how to find a method that turns #( $H $e $l $l $o ). into 'Hello'. Nor does MethodFinder new findMethodsByExampleInput: #( $H $e $l $l $o ) andExpectedResult: 'Hello'. work.
know how to do it(*), but I want to know how to utilize the Finder.
I guess the Finder simply can't find it? Am I missing something?
(*) For example, one way to do it i: String newFrom: #( $H $e $l $l $o ).. Another way would be: #($H $e $l $l $o) inject: '' into: [ :acc :el | acc, (el asString) ]. (though I would not expect the second way to be found by the Finder)
If you think about it like adding a collection of characters to a string rather than converting a collection to a string you can search for:
'' . #( $H $e $l $l $o ) . 'Hello'
in the Finder and you will get the results:
#( $H $e $l $l $o ) joinUsing: '' -> 'Hello'
'' , #( $H $e $l $l $o ) -> 'Hello'
'' join: #( $H $e $l $l $o ) -> 'Hello'
It can't find it.
Note however that there isn't a method that sent to #($H $e $l $l $o) would answer with 'Hello'. What there exists is a method that sent to String with argument #($H $e $l $l $o) will answer 'Hello'. So, the proper way to using MethodFinder in this case would have been
MethodFinder new
findMethodsByExampleInput: {String. #( $H $e $l $l $o )}
andExpectedResult: 'Hello'.
Unfortunately, the #newFrom: method is not explored (most class side methods aren't).
Related
I can bind containers to new names:
my %h;
my $p := %h{ "a" }{ "b" }{ "c" };
$p = 1;
say %h;
Which outputs expected:
{a => {b => {c => 1}}}
But what if I need to return such pointer from subroutine?
my %h;
sub get-pointer {
my $p := %h{ "a" }{ "b" }{ "c" };
return $p;
};
my $q := get-pointer();
$q = 1;
say %h;
Gives:
Cannot assign to a readonly variable or a value
That thing puzzles me - $p.WHERE and $q.WHERE give the same address, so why is it suddenly read-only?
Nevermind, I had some tunnel-vision moment and wanted aliases to behave as C pointers.
Found it clearly explained here at Raku Documentation.
The sub return will return values, not containers. Those are immutable
To return a mutable container, use return-rw.
Should be very simple, but I can't cope with it.
I want to match exactly the same number of as as bs. So, the following
my $input = 'aaabbbb';
$input ~~ m:ex/ ... /;
should produce:
aaabbb
aabb
ab
UPD: The following variants don't work, perhaps because of the :ex bug , mentioned in #smls's answer (but more likely because I made some mistakes?):
> my $input = "aaabbbb";
> .put for $input ~~ m:ex/ (a) * (b) * <?{ +$0 == +$1 }> /;
Nil
> .put for $input ~~ m:ex/ (a) + (b) + <?{+$0 == +$1}> /;
Nil
This one, with :ov and ?, works:
> my $input = "aaabbbb";
> .put for $input ~~ m:ov/ (a)+ (b)+? <?{+$0 == +$1}> /;
aaabbb
aabb
ab
UPD2: The following solution works with :ex as well, but I had to do it without <?...> assertion.
> $input = 'aaabbbb'
> $input ~~ m:ex/ (a) + (b) + { put $/ if +$0 == +$1 } /;
aaabbb
aabb
ab
my $input = "aaabbbb";
say .Str for $input ~~ m:ov/ (a)+ b ** {+$0} /;
Output:
aaabbb
aabb
ab
It's supposed to work with :ex instead of :ov, too - but Rakudo bug #130711 currently prevents that.
my $input = "aaabbbb";
say .Str for $input ~~ m:ov/ a <~~>? b /;
Works with ex too
my $input = "aaabbbb";
say .Str for $input ~~ m:ex/ a <~~>? b /;
Upd: explanation
<~~> means call myself recursively see Extensible metasyntax. (It is not yet fully implemented.)
Following (longer, but maybe clearer) example works too:
my $input = "aaabbbb";
my token anbn { a <&anbn>? b}
say .Str for $input ~~ m:ex/ <&anbn> /;
I've this error:
Use of uninitialized value $index in concatenation (.) or string at getdesc.pl line 43, <OctetsIn> line 2.
part of my code as follows:
my $select_sth = $dbh->prepare("SELECT Hid,Hostname,IP FROM Devices")
or die "$dbh->errstr";
$select_sth->execute() or die "$dbh->errstr";
while ( my $row_ref = $select_sth->fetchrow_hashref ) {
my $hostname = $row_ref->{'Hostname'};
if ( $hostname ne 'null' ) {
my $hid = $row_ref->{'Hid'};
my $ip = $row_ref->{'IP'};
my $desc = "null";
my $index = 0;
open( OctetsIn, "snmpwalk -v2c -c public $ip 1.3.6.1.2.1.18 |" )
or die "can't exec: $!";
while (<OctetsIn>) {
chomp;
print <OctetsIn> . "\n";
/IF-MIB::ifAlias.(\S+) = STRING: (\S+)/;
$index = $1;
$desc = $2;
$dbh->do(
"INSERT INTO Description (Hid,index,desc) Values ($hid,$index,'$desc')"
) or die "$dbh->errstr";
}
}
}
close(OctetsIn);
What error is there in my code? anyone knows how to fix the error ?
The error is on the line:
$dbh->do("INSERT INTO Description (Hid,index,desc) Values ($hid,$index,'$desc')") or die "$dbh->errstr";
You should test if regex was successful prior to assigning $1 to $index, ie.
# skip to next line if current did not match, as $1 and $2 are undefined
/IF-MIB::ifAlias.(\S+) = STRING: (\S+)/ or next;
There are three issues regarding your innermost while loop:
You're reading from the filehandle twice when trying to just print the current line:
while (<OctetsIn>) {
chomp;
print <OctetsIn> . "\n"; # Should be: print "$_\n";
Always verify that your regular expression matched before using capture variables.
/IF-MIB::ifAlias.(\S+) = STRING: (\S+)/;
$index = $1; # Will be undefined if regex doesn't match
$desc = $2;
Use placeholders and bind values instead of manually including values in a SQL statement:
Should aim to never interpolate values directly into a SQL statement like below:
"INSERT INTO Description (Hid,index,desc) Values ($hid,$index,'$desc')"
To clean up these three issues, I'd transform your inner while loop to something like the following.
while (<OctetsIn>) {
chomp;
print "$_\n";
if (my ($index, $desc) = /IF-MIB::ifAlias.(\S+) = STRING: (\S+)/) {
$dbh->do(
"INSERT INTO Description (Hid,index,desc) Values (?,?,?)",
undef, $hid, $index, $desc
) or die $dbh->errstr;
}
}
$index = $1;
your regexp doesn't match, so $1 is undef
I need to create scirpt which add users from list (containing user Display name) to group
Problem I encountered is :
when I issue command :
PS C:\Users\pskwarek> foreach ($a in $csv) {(get-aduser -f "DisplayName -like 'Piotr Skwarek'").samaccountname}
pskwarek
pskwarek
but if I use $a variable it doesnt work :
PS C:\Users\pskwarek> foreach ($a in $csv) {(get-aduser -f "DisplayName -like '$a.name'").samaccountname}
next step I would like to pass it to :
Add-adgroupMember -identity "groupname" -member samaccountname
but I can't make that single step
edit
PS C:\Users\pskwarek> $csv
Name
----
Piotr Skwarek
Renata Skwarek
You need to put $a.name in a subexpression:
foreach ($a in $csv)
{
$user = Get-ADUser -Filter "DisplayName -like '$($a.name)'"
Add-ADGroupMember groupname -Members $user
}
foreach ($a in $csv)
{
$sb = [scriptblock]::create("DisplayName -like '*$a*'")
(get-aduser -f $sb).SamAccountName
}
Try this:
foreach ($a in $csv) {(get-aduser -f ("DisplayName -like '{0}'" -f $a.name)).samaccountname}
You cannot use members of a variable directly in a string as you did.
Using string formatting (the '-f' syntax I proposed) always works.
Another solution would be to use a temporary variable to store $a.name
Try this:
$csv | % { Add-ADGroupMember -Identity "Groupname" -Member $_.Name }
How do I create a new variable each time a loop runs?
Something along the lines of
for ($i=1; $i -le 5; $i++)
{
$"var + $i" = $i
write-host $"var + $i
}
Use New-Variable and Get-Variable (mind available options including scopes). E.g.
for ($i=1; $i -le 5; $i++)
{
New-Variable -Name "var$i" -Value $i
Get-Variable -Name "var$i" -ValueOnly
}