Autohotkey script thats delete everything exept words in list - automation

I am new to Autohotkey and i will make a script that deletes everything exept words in document.
example:
> /Users/Admin/Application groups User Group Users HyRAM
> /Users/Admin/Application groups User Group Users PHAWorks
> /Users/Admin/Application groups User Group Users Proteus
> /Users/Admin/Application groups User Group Users RBMII
> /Users/Admin/Application groups User Group Users SAFETI
This needs to be the output:
- HyRAM
- PHAWorks
- Proteus
- RBMII
- SAFETI
How can we do this easily?
This is what i foundbut its not working how i want :)
vText := "abcdefghijklmnopqrstuvwxyz"
MsgBox, % RegExReplace(vText, "^.*?m")

Related

Select users who doesn't have any subscription active (Active Record Query)

I have association as following
A user has many subscriptions (only one subscription can have its status as active)
A subscription belongs to a user
I'm looking for an active record query that would help me select users whose all subscriptions are cancelled or in other words who doesn't have any active subscription.
Raw Sql queries are also welcome for this question.
We can do it in two ways
1st one:
User.where.not(id: Subscription.where(status: 'active').pluck(:user_id))
2nd one:
active_subscription_user_ids = Subscription.where(status: 'active').pluck(:user_id)
User.left_outer_joins(:subscriptions).where.not(id: active_subscription_user_ids)
You can do this with this query
User.where.not(id: Subscription.select(:user_id).where(status: 'Active'))
Please try below query
User.joins(:subscriptions).where('subscriptions.status = "active"').group('subscriptions.id').having("count(subscriptions.id) < 1")

Rails Select one random listings for premium users

In my rails app, I have Users and Listings. The Listings belong to a User. Listing has user_id and its filled with users id who is creating the listing.
A user can be a premium user, gold user or silver user.
What I want is for each premium user, select one random listing to show in premium listings.
I can do it in O(n**2) time or n+1 query as follow:
users_id = User.where(:role => "premium").pluck[:id]
final_array = Array.new
users_id.each do |id|
final_array << Listing.where(:user_id => id).sample(1)
end
final_array
Is there a better way of doing this?
You could try this:
listings = Listing.select(
<<~SQL
DISTINCT ON (users.id) users.id,
listings.*,
row_number() OVER (PARTITION BY users.id ORDER BY random())
SQL
)
.joins(:user)
.includes(:user)
.where(users: { role: :premium })
It gives a random Listing for every premium user.
It produces the only request to db and also it won't make an extra request for getting listing's user, so you are free to do something like this:
listings.each do |listing|
p listing.user
end
random_user_listings = []
User.includes(:listings).where(role: "premium").find_each do |user|
random_user_listings << user.listings.sample(1)
end
random_user_listings
To avoid N+1 query you need to combine them, perform query one time like this:
list = Listing.includes(:user).where(:role => "premium").sample(1)
Feel free to deal with list instead of Listing. Because now you're dealing with variable, not Query.
ids = list.pluck(:user_id).uniq
Getting array of ids like above and doing further steps as you did (but with list, not Listing)
Need to be noticed that, when you deal with Model you're dealing with QUERY. Avoiding doing that in loop statement.

Count total number of objects in list ordered by the number of associated objects

I have two models
class User
has_many :subscriptions
end
and
class Subscription
belongs_to :user
end
one one of my pages I would like to display a list of all users ordered by the number of subscriptions each user has. I am not to good with sql queries but I think that
list = Users.all.joins(:subscriptions).group("user.id").order("count(subscriptions.id) DESC")
dose the job. Now to my problem, when I try to count the total number of objects in list, using list.count, I get a hash with user.id and subscription count, like this
{11 => 5,
8 => 7,
1 => 11,
...}
not the total number of users in list.. .count works fine if I have a list sorted by for example user name (which is in the user table). I would really like to use .count since it in a module for pagination thats in a gem but any ideas is great!
Thanks!
We can just use a single query to finish this:
User.joins("LEFT OUTER JOIN ( SELECT user_id, COUNT(*) as num_subscriptions
FROM subscriptions
GROUP BY user_id
) AS temp
ON temp.user_id = users.id")
.order("temp.num_subscriptions DESC")
Basically, my idea is to try to query the number of subscription for each user_id in the subquery, then join with User. I used LEFT OUTER JOIN, because there will be several users which don't have any subscriptions
Improve option: You can define a scope inside User, it would be more beautiful for later usage:
user.rb
class User < ActiveRecord::Base
has_many :subscriptions
scope :sorted_by_num_subscriptions, -> {
joins("LEFT OUTER JOIN ( SELECT user_id, COUNT(*) as num_subscriptions
FROM subscriptions
GROUP BY user_id
) AS temp
ON temp.user_id = users.id")
.order("temp.num_subscriptions DESC")
}
end
Then just use it:
User.sorted_by_num_subscriptions
When grouping, the count method changes it's behavior and indeed, instead of returning the total count of records, it returns a hash of the counts for each group (see the docs for more info). So what you get with list.count is simply a hash of the subscription counts for each user.
So, your query is correct and all you need is to sum up the individual counts in the groups. This can be done simply by:
total_count = list.count.values.sum
If it is the pagination code that calls just a bare count that makes the issue, usually the pagination code is able to accept a parameter with total count. For example, will_paginate accepts the total_entries parameter, so you should be able to pass it the total count like this:
list.paginate(page: 2, total_entries: list.count.values.sum)

While inside LOAD / Section Access in Qlikview

I have an hierarchy Table which I try to add in QlikView in order to use RESTRICTION (Section Access) on users to show/hide data.
On the Table, I have the user REFERENCE, with an ID which is linked to an IDPARENT in order to have an hierarchy.
I want that all the user "on top" of the leaf user (the last one in the hierarchy) could have access on his data.
In order to do this, I use this SQL query :
Select
REFERENCE,
LEVEL-1 "LEVEL",
from HIERARCHYTABLE
start with TYPE='VD' //Start hierarchy with all the users with type='VD'
connect by ID = prior IDPARENT;
It return :
VD254 0
IG203 1
GR203 2
VD255 0
IG232 1
GR258 2
IG235 1
GR259 2
-> So IG203 and GR203 can access to the VD254 data
And IG232, GR258, IG235 and GR259 can access to the VD255
With that, I have all my REFERENCE associated to the LEVEL (Here the user on the bottom is the level "0").
I want my AUTHENTIFICATE table to be like :
ACCESS, REFERENCE, PASSWORD, RESTRICTION
ADMIN, ADMIN, ADMIN, *
USER, VD254, VD254, VD254
USER, VD254, VD254, VD255 // Here the user VD254 can access to his data and the VD255 user data
So I was thinking of that when I have a 0 LEVEL, all the next users would have the REFERENCE of the 0 level in RESTRICTION until the loop see another 0 LEVEL :
But I can't make it works :
. The IterNo() is alway at '0' so the IF condition is alway true with the wrong value !
. It add an infinite number of rows to AUTH, so LEVEL <> '0' is never true...
AUTHRESIDENT:
LOAD
REFERENCE,
LEVEL,
Select
REFERENCE,
LEVEL-1 "LEVEL",
from HIERARCHYTABLE
start with TYPE='VD' //Start hierarchy with all the users with type='VD'
connect by ID = prior IDPARENT;
Let vRowCount = NoOfRows('AUTHRESIDENT');
DO WHILE (IterNo() <> vRowCount)
IF peek('LEVEL', IterNo(), 'AUTHRESIDENT')='0' THEN
Let vNumVd = peek('REFERENCE', IterNo(), 'AUTHRESIDENT');
AUTH:
LOAD
'USER' as ACCESS,
'00211' as REFERENCE, // Only to test
'00211' as PASSWORD,
$(vNumVd) as RESTRICTION
RESIDENT AUTHRESIDENT
WHILE LEVEL <> '0';
ENDIF;
LOOP;
Thank you a lot for your help !
I finally found the solution with the unbalanced hierarchy, here is my code for people in the same case :
HIERARCHY:
HierarchyBelongsTo(IDNOEUD, IDNOEUDPARENT, REFERENCE, TreeID, TreeName)
LOAD
REFERENCE,
IDNOEUD,
IDNOEUDPARENT,
Select
REFERENCE,
IDNOEUD,
IDNOEUDPARENT,
from HIERARCHYTABLE;
Trees:
LOAD
*,
Upper(TreeName) as PERMISSION,
REFERENCE as MYPERMISSIONFIELD // Field which is the filter
Resident HIERARCHY;
Drop Table HIERARCHY;
Section Access;
AUTH:
LOAD * INLINE [
ACCESS, USERID, PASSWORD, PERMISSION
ADMIN, ADMIN, ADMIN, * // To add the ADMIN !
];
AUTH:
LOAD
'USER' as ACCESS,
REFERENCE as USERID,
REFERENCE as PASSWORD,
UPPER(REFERENCE) as PERMISSION;
SELECT
REFERENCE
FROM HIERARCHYTABLE;
Section Application;
Then the PERMISSION is linked to all the MYPERMISSIONFIELD.

Problems with Facebook fql.query

I have some problems with this fql.query method:
select music
from user
where uid = ......
and music in (select music
from user
where uid = ......
)
I want to obtain common music interest between two users; it works with queries like this
select uid
from user
where uid = ......
and uid in (select uid
from user
where uid = ......
)
I think the problem is that the second query returns an integer and the first one returns a string array. Can anyone help me with this?
(Excuse my bad English! I'm from Spain ;) )
Does not the first query already gives you your answer?
I mean it does the following :
-- Display only music field
select music
from user
where uid = <first_USER> -- Selection of your first user
and music in ( -- We want to macth the string with another
select music -- Selects only the music field
from user
where uid = <second_USER> -- Selection of your second user
)
So you gets a music name that match in two user records.
Your second query seems a bit odd to me :
select uid
from user
where uid = <first_USER?>
and uid in (select uid
from user
where uid = <second_USER?>
)
Which basically translates to find the user_id matching user one and user two. Thing that does never happen since each user has it's own uid.
By the way, I don't know how facebook handles music string, but it may appear that user don't write their favorite music name in a common spelling and/or format. That may impede your matching system.
I tried this:
select music
from user
where uid=UID1
AND music IN (SELECT music from user where uid=UID2)
And it works fine when the UID1 music string matches exactly the UID2's one.