Ruby Script that doesn't print duplicate lines - ruby

What can i add to this script that will cause it to not print out the duplicate lines from the txt file
The Script is
class TestKeyword
file = File.new("test.txt", "r")
while (line = file.gets)
if line['MAY_DAY']
date = line[/\w+ +\d+ +\d+:\d+:\d+/]
puts "#{date}"
end
end
end
this is the test file:
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
Currently when i execute the script i get the following(which is the date and time of the lines that have the keyword "MAY_DAY":
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
The output i need is:
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
Which doesn't have the duplicates

You're going to have to remember what lines you have already output with a little array, e.g.
class TestKeyword
found = []
file = File.new("test.txt", "r")
while (line = file.gets)
if line['MAY_DAY']
date = line[/\w+ +\d+ +\d+:\d+:\d+/]
if !found.include? date
found << date
puts "#{counter}: #{date}"
end
end
end
end
See what I'm doing there? If the date isn't in the array, we add it to it and output the date. Otherwise we ignore it.
Edit: if you want to be a bit more advanced you can use a Set rather than an array. Sets are designed for fast lookup of unique elements. If the only question you want to ask is 'is this element in this set?' and you don't care about order, use a Set. To do that, just change this line:
found = []
To this:
found = Set.new

If the file isn't huge, this will print out the unique lines that match:
file.readlines.select{|l| l.include? "MAY_DAY"}.uniq
It doesn't apply the counter, but that is easily added.

Related

How to check if a date value inside an hash is bigger than a reference date

I'm writing a validation and I have an hash with this structure
elements.map{ |e| [e.id,e.coverable.published_at] }.to_h
=> {305=>Fri, 17 Apr 2020 15:23:00 CEST +02:00,
306=>Fri, 17 Apr 2020 13:00:00 CEST +02:00,
307=>Fri, 17 Apr 2020 09:20:00 CEST +02:00,
308=>Fri, 17 Apr 2020 12:59:00 CEST +02:00,
309=>Fri, 17 Apr 2020 11:39:00 CEST +02:00}
I have a reference date...
published_at
=> Mon, 04 May 2020 23:51:00 CEST +02:00
I have to check if any of the element has a published_at datetime value bigger than my published_at.
Is there a short way to do that?
Try something like this
elements.any? { |e| e.coverable.published_at > your_published_at }
In case you need the element which passes the condition use find
element = elements.find { |e| e.coverable.published_at > your_published_at }
# if element is not nil such element is present

FORTRAN : maxloc

my data looks like
JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
22.60 24.60 30.60 34.60 36.20 35.70 32.10 30.20 31.40 31.60 28.00 24.80
25.40 27.60 32.40 34.60 36.50 38.10 31.70 31.40 30.30 30.20 27.00 23.90
and there are like hundreds of rows! I want to find a maximum value in each row and write it in different column next to data along with month
so my out put will be
36.20 MAY
38.10 JUN
.
.
I want to use maxloc function, but i have no idea how to use it!
Try
index = maxloc(myTable(3,:))
print *, myTable((/1,3/), index)
It should select the highest value from the third row and display the first and third value at this index.

How do I create a cron expression running in Kibana on weekday?

I would like my watcher to run from Monday to Friday only. So I'm trying to use this schedule:
"trigger": {
"schedule" : { "cron" : "0 0 0/4 * * MON-FRI" }
},
"input": {
...
However, I'm getting
Error
Watcher: [parse_exception] could not parse [cron] schedule
when I'm trying to save the watcher. Removing MON-FRI does helps but I need it.
This expression works:
0 0 0/4 ? * MON-FRI
But I'm not sure I understand why ? is required for either the day_of_week or day_of_month
Thank you!
I believe this is what you are looking for:
"0 0 0/4 ? * MON-FRI"
You can use croneval to check your cron expressions 1:
$ /usr/share/elasticsearch/bin/x-pack/croneval "0 0 0/4 ? * MON-FRI"
Valid!
Now is [Mon, 20 Aug 2018 13:32:26]
Here are the next 10 times this cron expression will trigger:
1. Mon, 20 Aug 2018 09:00:00
2. Mon, 20 Aug 2018 13:00:00
3. Mon, 20 Aug 2018 17:00:00
4. Mon, 20 Aug 2018 21:00:00
5. Tue, 21 Aug 2018 01:00:00
6. Tue, 21 Aug 2018 05:00:00
7. Tue, 21 Aug 2018 09:00:00
8. Tue, 21 Aug 2018 13:00:00
9. Tue, 21 Aug 2018 17:00:00
10. Tue, 21 Aug 2018 21:00:00
For the first expression you'll get following java exception:
java.lang.IllegalArgumentException: support for specifying both a day-of-week AND a day-of-month parameter is not implemented.
You can also use Crontab guru to get human readable descriptions like:
At every minute past every 4th hour from 0 through 23 on every day-of-week from Monday through Friday.
The question mark means 'No Specific value'. From the documentation on Quartz's website:
? (“no specific value”) - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don’t care what day of the week that happens to be, I would put “10” in the day-of-month field, and “?” in the day-of-week field. See the examples below for clarification.
http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
I suppose since you want your schedule to run every 4 hours, mon-fri, the actual day of the month is irrelevant, so the ? specifies that. * on teh other hand would be 'all values' which would not make sense since you are specifying only mon-fri for day of the week.
Hope that helps!

Sort a hash with key as month date with many values for the same month

I create a hash with months as keys and timelaps as values
biens_delai[bien_date.mon] = b.delai
I get this result without month parsing
{Wed, 18 Jan 2017=>3.0, Sat, 25 Feb 2017=>2.0, Fri, 17 Mar 2017=>3.0, Sat, 25 Mar 2017=>5.0, Tue, 18 Apr 2017=>2.0, Thu, 29 Jun 2017=>2.0}
In March i have 2 values but when i parse by month i get the most high value and i want a addition of 2 values for March not the most high
{1=>3.0, 2=>2.0, 3=>5.0, 4=>2.0, 6=>2.0}
That's not the high value which you are getting, the values are getting overwritten, try the following
biens_delai[bien_date.mon] = biens_delai[bien_date.mon].to_f + b.delai

Ruby: Grouping a date range by year and month

I am creating a range for each month in my example range.
example_range = (Time.zone.today..2.years.from_now)
Output should look like so:
=> [Wed, 03 Aug 2016..Wed, 31 Aug 2016, Thu, 01 Sep 2016..Fri,
30 Sep 2016, Sat, 01 Oct 2016..Mon, 03 Oct 2016, ...]
At the moment I'm doing this, which doesn't work for ranges longer than a year, because the grouping will put January '16 and January '17 in one group.
example_range.group_by(&:month).each { |_, month| month.first..month.last }
I also tried this, but ruby segfaults on this for some reason...
example_range.group_by(&:year).map{ |ary| ary.group_by(&:month)}
Does anyone know a more beautiful (or at least working) way of doing this?
How is this:
example_range.group_by {|date| [date.year, date.month] }.map {|_, month| month.first..month.last }
If you are using Active Support (Rails), this will also work:
example_range.group_by(&:beginning_of_month).map {|_, month| month.first..month.last }
The best solution I think is this:
example_range.group_by {|date| date.month.to_s + "-" + date.year.to_s}
You can adjust the way you need.

Resources