Sorting hash kv pairs - sorting

my %hash =
two => 2,
three => 3,
one => 1,
;
for %hash.sort(*.key)>>.kv -> ($key, $value) {
say "'$key' => '$value'";
}
Is %hash.sort({.key})>>.kv equivalent to above sort?
Why this sort doesn't work without hyper >> hint?

The sort method is returning a List of Pairs.
Since calling .kv on the list returns a list of index, Pair lists, which you don't want; you can't just call .kv. So you have to pull out the key and value from the Pair objects in the list individually by calling the .kv method on each of them, which >>.kv does.
You could also have used .map(*.kv) instead.
The >>.kv syntax allows an implementation to spread the work over multiple threads if it makes sense to do so.
( Currently Rakudo just does the work in a semi-random order to prevent people from using the feature wrong )
There is an alternate way of writing the loop by extracting the attributes using adverbs in the sub-signature:
for %hash.sort -> (:$key, :$value) {
say "'$key' => '$value'";
}
for %hash.sort -> $pair (:$key, :$value) {
say $pair;
say $key === $pair.key and $value === $pair.value; # True␤
}
# :$key is short for :key($key)
for %hash.sort -> (:key($k), :value($v)) {
say "'$k' => '$v'";
}
This can be useful on other objects which don't have a method for creating a list of their public attributes
class C { has $.a; has $.b; has $.c; has $!private-value }
my $c = 5;
my $obj = C.new(:a<A>,:b(1),:$c);
given $obj -> ( :$a, :b($b), :$c) ) {
say "$a $b $c";
}
# ignore $.a by using an unnamed scalar
given $obj -> ( :a($), :$b, :$c ) { ... }
# places any unspecified public attributes in %others
given $obj -> ( :$a, :$b, *%others ) {
.say for keys %others; # c␤
}
# ignores any unspecified attributes
# useful to allow subclasses to add more attributes
# or to just drop any values you don't care about
given $obj -> ( :$a, :$b, *% ) { ... }
# fails because it doesn't handle the public c attribute
# in the sub-signature
given $obj -> ( :$a, :$b ) { ... }
That is only the beginning of what's possible with signatures.
All of the following is also allowed in subroutine and method signatures, optional, and completely overkill for this example.
It is really useful in multi subs and multi methods for restricting the possible candidates.
for 'one' => 1, 1/3
->
# Type is an alias to the object type
::Type Any $_ # Any is the default type requirement
# the public attributes of the object
(
::A-Type Any :key( :numerator( $a ) ),
::B-Type Any :value( :denominator( $b ) ) where $b >= 1,
)
{
my Type $obj = $_; # new variable declared as having the same type
my A-Type $new-a = $a;
my B-Type $new-b = $b;
# could have used $_.^name or .^name instead of Type.^name
# so you don't actually have to add the alias to the signature
# to get the name of the arguments type
say Type.^name, ' ', $_;
say ' ', A-Type.^name, ' ', $a;
say ' ', B-Type.^name, ' ', $b;
}
Pair one => 1
Str one
Int 1
Rat 0.333333
Int 1
Int 3
As to using .sort({.key}), yes that is basically the same thing, as sort accepts anything Callable there.
I would like to point out that you didn't even need to provide an argument to sort because it's default is even smarter than what you gave it.
Perl 6 has many ways of creating and accessing Callable things. So any of the following would have worked:
*.key
{ .key } # { $_.key }
-> $_ { .key } # basically what the previous line turns into
{ $^placeholder-var.key }
sub ($_) { .key }
&a-subroutine-reference # you would have to create the subroutine though
Also since all of the normal operators are actually subroutines, you could use them in other places where you need a Callable. ( I can't think of one that works in that spot though )
&infix:<+> # the subroutines responsible for the numeric addition operator
&[+] # ditto
&prefix:<++>
&postfix:<++>
# etc

As far as I can see, the ony difference between the two versions is use of a block with implicit $_ parameter instead of using a Whatever-Star, so they are indeed equivalent.
This is Perl, so There Is More Than One Way to Do It:
*.key
{ .key }
{ $^arg.key }
-> $arg { $arg.key }
Why this sort doesn't work without hyper >> hint?
sort coerces the hash to a list of pairs, and that's what you'll get:
say %hash.sort(*.key).perl;
# ("one" => "1", "three" => "3", "two" => "2")
To get rid of the pairs, you need to iterate over the list and call .kv on each one:
say %hash.sort(*.key)>>.kv.perl;
# (("one", "1"), ("three", "3"), ("two", "2"))
say %hash.sort(*.key).map(*.kv).perl;
# (("one", "1"), ("three", "3"), ("two", "2"))
You could coerce to Hash before calling .kv:
say %hash.sort(*.key).hash.kv.perl;
# ("one", "1", "three", "3", "two", "2")
but this would of course defeat the purpose of the excercise as hash ordering cannot be relied on.
You may have noticed that you'll get different results depending on how exactly you write the code. If there no trailing .list, what you get is actually a Parcel and not a List, but semantics have not been finalized.
Note that even though the returned objects all perlify with simple parentheses, some are parcels and some are lists, which you can check by calling .WHAT. This is still a work in progress.
Also note the inner parentheses in some of these variants, which you can get rid of with a call to .flat. If you do so, you can use -> $key, $value as signature of your for loop instead of -> ($key, $value) (or, more explicitly, -> $anon ($key, $value)) which uses signature binding to unpack the parcels.
Instead of using .kv, you could use the same approach to unpack the pair objects instead:
for %hash.sort(*.key) -> (:$key, :$value) {
say "'$key' => '$value'";
}

Related

How do I sort Discord.Collection by keys?

I have a Discord.Collection of key-value pairs that stores the information about the number of commands in folders. It looks something like this:
debug - 2
utility - 2
fun - 3
best - 4
replies - 3
I want to sort the collection by folder names (keys) alphabetically (ascending). However, Discord.Collection.sort() sorts ascending by values, meaning my output is debug, utility, fun, replies, best instead of desired best, debug, fun, replies, utility output. Because Discord.Collection extends Map, I looked up js map documentation, but there is no .sort() method. I also looked up sorting in StackOverflow and google, but I only found answers regarding value sorting (or answer in different coding language, which I failed to translate).
I know .sort() accepts lambda expression as parameter, but I don't know how to use it for key sorting - I only ever used it for value sorting.
I don't think the code is necessary for this question, but in case you need to visualise my problem, here is creation of my collection:
const FolderCollection = new Discord.Collection();
//commandList is a Collection of key: command.name, value: {command: command, folder: folder}
commandList.each((cmd) => {
if (FolderCollection.has(cmd.folder)) {
FolderCollection.set(
cmd.folder,
FolderCollection.get(cmd.folder) + 1
);
} else {
FolderCollection.set(cmd.folder, 1);
}
});
Here I want to use my sorted FolderCollection:
FolderCollection.sort().each((cmdCount, categoryName) => {
//...do stuff
});
Feel free to ask for any additional details, but I believe this is all you need to help me with my issue (either direct answer, or link to documentation).
You could use Collection#entries() in a spread operator to turn the collection into an array with the structure [[key, value]], sort that because you'll have access to the whole array (key and value), and then convert it back into a collection if needed. Here's an example with a Map.
// let's say my objective is to sort these
// from lowest to highest relative to the key
// so the result should be:
// Map (3) { 1 => 'bar', 2 => 'baz', 3 => 'foo' }
const map = new Map([[3, 'foo'], [1, 'bar'], [2, 'baz']]);
// make use of [...iterable] to turn an iterable
// into an array
const entries = [...map.entries()];
console.log('Entries: ', entries);
// use array destructuring to access the
// first element of both arrays
entries.sort(([a], [b]) => a - b);
console.log('Sorted Entries: ', entries);
// convert it back to a map (or collection)
// if needed
const newMap = new Map(entries);

How can you assign values to a Hash key without concomitant boxing (i.e. itemization)?

Coming from this SO question, I'm trying to have a List (or non-scalar thing, in general) as the value assigned to a Hash key, this way:
my %syns-by-name does Associative[Str,List] = Bq => ("Bq", "becquerel", "becquerels");
my Str #syns = %syns-by-name<Bq>;
That does not work, however. Lists are itemized before being assigned, so the value is always a Scalar. You need to do a workaround to actually make this work:
my %syns-by-name does Associative[Str,List] = Bq => ("Bq", "becquerel", "becquerels");
my #list := <C coulomb coulombs>;
%syns-by-name<C> := #list;
my Str #syns = %syns-by-name<C>;
say #syns;
This returns what we were looking for, a list. However, how could we do that directly on the assignment and convince a list is a list and not an itemized list?
Assuming you don't need mutation afterwards, use a Map instead of a Hash.
my %syns-by-name is Map = Bq => ("Bq", "becquerel", "becquerels");
my Str #syns = %syns-by-name<Bq>;
say #syns; # [Bq becquerel becquerels]
Since there's no expectation that entries in a Map are assignable, it doesn't create Scalar containers for the values.
How about:
role deconting {
method AT-KEY(\key) {
callsame<>
}
}
my %h does deconting = a => <a b c>;
dd $_ for %h<a>; # "a"␤"b"␤"c"␤
This makes sure that the hash that does the "deconting" role will always return whatever is in the hash decontainerized.
Making it decontainerized on assignment can also be done, but is a lot more tricky as that would need tweaking of at least two methods: STORE and ASSIGN-KEY.
Despite the excellent answers from #Jonathan_Worthington and #Elizabeth_Mattijsen, I wanted to post the code below, which utilizes simple decontainerization:
~$ raku
Welcome to 𝐑𝐚𝐤𝐮𝐝𝐨™ v2020.10.
Implementing the 𝐑𝐚𝐤𝐮™ programming language v6.d.
Built on MoarVM version 2020.10.
To exit type 'exit' or '^D'
> my %syns-by-name = Bq => ("Bq", "becquerel", "becquerels");
{Bq => (Bq becquerel becquerels)}
> my Str #syns = %syns-by-name<Bq>;
Type check failed in assignment to #syns; expected Str but got List (("Bq", "becquerel", ...)
in block <unit> at <unknown file> line 1
> my Str #syns = %syns-by-name<Bq>[];
[Bq becquerel becquerels]
>
I gather there is an academic question here as to how the variable is defined versus how the values of the variable are accessed. However I don't want a casual reader to conclude that Raku is lacking functionality vis-à-vis hashes and lists.
> my %syns-by-name = Bq => ("Bq", "becquerel", "becquerels");
{Bq => (Bq becquerel becquerels)}
> dd $_ for %syns-by-name<Bq>[]
"Bq"
"becquerel"
"becquerels"
Nil
> my $list = <C coulomb coulombs>;
(C coulomb coulombs)
> say $list.WHAT
(List)
> %syns-by-name<C> = $list
(C coulomb coulombs)
> dd $_ for %syns-by-name<C>[]
"C"
"coulomb"
"coulombs"
Nil
>
I hope this answer isn't superfluous and casual readers will benefit. Thank you.

Are single element lists allowed in perl?

I'm trying to dynamically create a list of values from an AJAX request (using Catalyst), like this:
my #listofvalues = #{$params->{parameter_with_many_values}};
Then I loop through the list to make database insertions (one for each value). Since I need to loop through various lists like the one above, I need to access the index of the list. I am currently doing it like this:
foreach my $key (0 .. $#listofvalues){
$table_model->new({
field1 => $listofvalues[$key],
field2 => $x,
field3 => $another_listofvalues[$key]
field4 => $yet_another_listofvalues[$key]
});
}
This seems to work fine when two or more elements are received in the request. Whenever a single element is received, I get an error like
[error] Caught exception in pbitdb::Controller::Subjects->add "Can't use string ("1") as an ARRAY ref while "strict refs" in use at /home/lioneluranl/svn/pbitdb/pbitdb/script/../lib/pbitdb/Controller/Subjects.pm line 119."
Where, in this case, 1 is the value received and line 119 is the line where #listofvalues is being declared.
Now I've tried several to workaround this issue, but haven't found anything that works both ways (for a single or for various values). Any suggestions?
Yes, single element lists are OK in Perl, as are arrays and references to such arrays.
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
sub new {
print Dumper \#_;
}
my $table_model = 'main';
for my $values ( [ 'a' .. 'c' ],
[ 'd' ]
) {
my $params = { parameter_with_many_values => $values };
my #listofvalues = #{ $params->{parameter_with_many_values} };
my #another_listofvalues = map uc, #listofvalues;
for my $key (0 .. $#listofvalues) {
my $x = rand;
$table_model->new({
field1 => $listofvalues[$key],
field2 => $x,
field3 => $another_listofvalues[$key]
});
}
}
How do you populate $params->{parameter_with_many_values}?
Update
It seems Catalyst::Request should mention that their "safe" parameters should be handled as follows:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
for my $params ( { param_with_many_values => 'a' },
{ param_with_many_values => [ 'a' .. 'e' ] },
{ something => 'else' }
) {
my $value_or_values = $params->{param_with_many_values};
my #list_of_values = ref $value_or_values ? #$value_or_values
: defined $value_or_values ? $value_or_values
: ();
print Dumper \#list_of_values;
}
First of all, you are asking about arrays (a type of variable), not lists (a vague term which can take on numerous definitions, none of which are pertinent here).
Yes, you can have an array with only one element.
$ perl -e'my #a = "ele"; CORE::say 0+#a; CORE::say for #a;'
1
ele
That's not the problem. The problem is that
#{$params->{parameter_with_many_values}}
expects
$params->{parameter_with_many_values}
to contain a reference to an array, but it contains 1 instead. It was probably set using
$params->{parameter_with_many_values} = #a; # Assigns number of elements
instead of
$params->{parameter_with_many_values} = \#a;

Check Registry Version using Perl

I need to go to the registry and check a programs installed version. I am using perl to a whole lot of others things but the registry checking part isn't working. The program version has to be 9.7 and up so it could be 9.8 or 9.7.5.
When I install the program it shows 9.7.4 but I just need the 9.7 to be checked.
Bellow is me going to DisplayVersion which is a REG_SZ which shows 9.7.4.
OR
I could use VersionMajor and VersionMinor together which is a REG_DWORD. Which for Major is 9 and Minor is 7.
$ProgVersion= `$rootpath\\system32\\reg\.exe query \\\\$ASSET\\HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{9ACB414D-9347-40B6-A453-5EFB2DB59DFA} \/v DisplayVersion`;
if ($ProgVersion == /9.7/)
This doesn't work I could make it 9.200 and it still works. I tried to use this instead and it still wouldn't work. This next part is assuming that a new client needs to be install if it goes from 9.7. I was trying to use Great than or equal to, but it didn't work.
$ProgVersionMajor= `$rootpath\\system32\\reg\.exe query \\\\$ASSET\\HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{9ACB414D-9347-40B6-A453-5EFB2DB59DFA} \/v VersionMajor`;
$ProgVersionMinor= `$rootpath\\system32\\reg\.exe query \\\\$ASSET\\HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{9ACB414D-9347-40B6-A453-5EFB2DB59DFA} \/v VersionMinor`;
if (($ProgVersionMajor=~ /9/) && ($ProgVersionMinor=~ /7/))
Any help on doing this correctly or fixing what I am doing.
Several things:
You don't mention it, but are you using the Perl module Win32::TieRegistry? If not, you should. It'll make handling the Windows registry much easier.
In the Perl documentation, you can look at Version String under Scalar Value Constructors. This will make manipulating version strings much, much easier. Version strings have either more than one decimal place in them, or start with the letter v. I always prefix them with v to make it obvious what it is.
Here's a sample program below showing you how they can be used in comparisons:
#! /usr/bin/env perl
#
use strict;
use warnings;
my $version = v4.10.3;
for my $testVersion (v3.5.2, v4.4.1, v5.0.1) {
if ($version gt $testVersion) {
printf qq(Version %vd is greater than test %vd\n), $version, $testVersion;
}
else {
printf qq(Version %vd is less than test %vd\n), $version, $testVersion;
}
}
Note that I can't just print version strings. I have to use printf and sprintf and use the %vd vector decimal format to print them out. Printing version strings via a regular print statement can cause all sorts of havoc since they're really unicode representations. You put them in a print statement and you don't know what you're getting.
Also notice that you do not put quotes around them! Otherwise, you'll just make them regular strings.
NEW ANSWER
I was trying to find a way to convert a string into a v-string without downloading an optional package like Perl::Version or (Version), and I suddenly read that v-strings are deprecated, and I don't want to use a deprecated feature.
So, let's try something else...
We could simply divide up version numbers into their constituent components as arrays:
v1.2.3 => $version[0] = 1, $version[1] = 2, $version[2] = 3
By using the following bit of code:
my #version = split /\./, "9.7.5";
my #minVersion = split /\./, "9.7"
Now, we can each part of the version string against the other. In the above example, I compare the 9 of #version with the 9 of #version, etc. If #version was 9.6 I would have compared the 6 in #version against the 7 in #minVersion and quickly discovered that #minVersion is a higher version number. However, in both the second parts are 7. I then look at the third section. Whoops! #minVersion consists of only two sections. Thus, #version is bigger.
Here's a subroutine that does the comparison. Note that I also verify that each section is an integer via the /^\d+$/ regular expression. My subroutine can return four values:
0: Both are the same size
1: First Number is bigger
2: Second Number is bigger
undef: There is something wrong.
Here's the program:
my $minVersion = "10.3.1.3";
my $userVersion = "10.3.2";
# Create the version arrays
my $result = compare($minVersion, $userVersion);
if (not defined $results) {
print "Non-version string detected!\n";
}
elsif ($result == 0) {
print "$minVersion and $userVersion are the same\n";
}
elsif ($result == 1) {
print "$minVersion is bigger than $userVersion\n";
}
elsif ($result == 2) {
print "$userVersion is bigger than $minVersion\n";
}
else {
print "Something is wrong\n";
}
sub compare {
my $version1 = shift;
my $version2 = shift;
my #versionList1 = split /\./, $version1;
my #versionList2 = split /\./, $version2;
my $result;
while (1) {
# Shift off the first value for comparison
# Returns undef if there are no more values to parse
my $versionCompare1 = shift #versionList1;
my $versionCompare2 = shift #versionList2;
# If both are empty, Versions Matched
if (not defined $versionCompare1 and not defined $versionCompare2) {
return 0;
}
# If $versionCompare1 is empty $version2 is bigger
if (not defined $versionCompare1) {
return 2;
}
# If $versionCompare2 is empty $version1 is bigger
if (not defined $versionCompare2) {
return 1;
}
# Make sure both are numeric or else there's an error
if ($versionCompare1 !~ /\^d+$/ or $versionCompare2 !~ /\^\d+$/) {
return;
}
if ($versionCompare1 > $versionCompare2) {
return 1;
}
if ($versionCompare2 > $versionCompare1) {
return 2;
}
}
}
Using Win32::TieRegistry
You said in your answer you didn't use Win32::TieRegistry. I just want to show you what it can do for the readability of your program:
Your Way
$ProgVersion= `$rootpath\\system32\\reg\.exe query \\\\$ASSET\\HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{9ACB414D-9347-40B6-A453-5EFB2DB59DFA} \/v DisplayVersion`;
With Win32::TieRegistry
use Win32::TieRegistry ( TiedHash => '%RegHash', DWordsToHex => 0 );
my $key = $TiedHash->{LMachine}->{Software}->{Wow6432Node}->{Microsoft}->{Windows}->{CurrentVersion}->{Uninstall}->{9ACB414D-9347-40B6-A453-5EFB2DB59DFA}->{Version};
my $programValue = $key->GetValue;
my $stringValue = unpack("L", $programValue);
Or, you can split it up:
my $MSSoftware = $TiedHash->{LMachine}->{Software}->{Wow6432Node}->{Microsoft};
my $uninstall = $MSSoftware->{Windows}->{CurrentVersion}->{Uninstall};
my $programVersion = $uninstall->{9ACB414D-9347-40B6-A453-5EFB2DB59DFA}->{Version};
See how much easier that's to read. You can also use this to test keys too.
(Word 'o Warning: I don't have a Windows machine in front of me, so I didn't exactly check the validity of the code. Try playing around with it and see what you get.)

Handling unions, subsets and supersets in Scala

I need to write a code snippet that would compare multiple arrays and produce the set that match the data in those arrays, produce the data set only in array A, but not in array B,C,D,in array B but not in A,C,D, being able to handle any number of arrays (i.e. dynamically looped). The code should utilize anonymous functions in Scala (i.e. not like a regular array looping like in Java).
Sounds like homework to me. By far the easiest way to do this is to throw the contents of array A into a Set and use the remove operation. This would be O(n) where n = \sum { |A|, |B|, |C|, ... }. The following solution works for arbitrary Iterable, not just Array:
def compareArrays[A](arrs: List[Iterable[A]]) = arrs match {
case a :: tail => {
val set = a.foldLeft(Set[A]()) { _ + _ }
tail.foldLeft(set) { _ -- _ }
}
case Nil => Nil
}

Resources