Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Jabor
Jul 16, 2010

#1 Loser at SpaceChem
If you find it way easier to explain how you'd do it in code than to write an appropriate regex, that's a big hint that a regex is the wrong tool and you should just write code.

But this isn't too hard to do with a regex using a lookahead:
code:
/^(?=.*a).{2}[^a]/
Here the lookahead makes sure that the line contains at least one a, then the rest of the expression makes sure it doesn't have an a in the third position.

If you have lines that are shorter than the number of characters given, then this won't match them, and you'll need to change the expression.

If you have a more complex condition that matches everything you want to exclude, sometimes a negative lookahead is easier to write:
code:
/^(?!.{2}a).*a)/

Adbot
ADBOT LOVES YOU

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


This is mostly curiosity. In practice I just use multiple regexes and only match the lines that match all of them.

Jaded Burnout
Jul 10, 2004


I'm having a bit of confusion with some javascript event bubbling. At least I reckon that's what the issue is.

I have a structure like this:
code:
<div class="waveform">
  <span class="marker"></span>
  <div class="segments">
    <div class="segment></div>
  </div>
</div>
and the user can opt to "cut" which splits the current segment into two.

I'm tracking the marker position using a combination of a `click` and a `mousemove` (when button is pressed) event handler set on the top level "waveform" element, which then use `event.offsetX` to figure out the position.

The issue I'm having is that this is generated correctly when there's one full segment, but once it splits `event.offsetX` becomes relative to each "segment" element. I'm also occasionally seeing "0" as the `event.offsetX` even when there's just one segment.

My assumption here is that the event handler is picking up clicks from the segments and/or the marker elements, and not receiving any of its own.

If that's correct, how do I go about making sure that the "waveform" element (the one with the handler on it) is the only one this callback is called for? i.e. so that the `event.offsetX` value is always correct. Do I need to explicitly add handlers to all child elements telling them to ignore it? I'd rather not fix it with maths if there's a good way to sort out the events themselves.

ynohtna
Feb 16, 2007

backwoods compatible
Illegal Hen
Old-school method: addEventListener with useCapture true, to get the event in the capturing phase before being dispatched to child elements. Probably with calling preventDefault and stopPropagation to block the browser from desperately searching for more ways of wasting cycles.

Cutting edge CSS: pointer-events: none on the child elements.

ynohtna fucked around with this message at 12:26 on Aug 15, 2022

Jaded Burnout
Jul 10, 2004


Oh are they propagated downwards?

ynohtna
Feb 16, 2007

backwoods compatible
Illegal Hen
I think this explains the stuff far better than I'll be able to, I always get it wrong without/with some references and experimentation:

https://www.quirksmode.org/js/events_order.html

Jaded Burnout
Jul 10, 2004


ynohtna posted:

I think this explains the stuff far better than I'll be able to, I always get it wrong without/with some references and experimentation:

https://www.quirksmode.org/js/events_order.html

That does explain it, thanks. Looks like I'm still SoL though because `offsetX` remains relative to the original element, so I'm going to have to do some maths anyway.

Saoshyant
Oct 26, 2010

:hmmorks: :orks:


Any of you fellows have any experience in Forth? Like, where is it used these days (practical projects) if at all, if it's worth checking out, how hard it is, etc?

I read a while ago that Forth is the language you learn if you want to know how computers really work and that always intrigued me.

But I didn't think much about it until today when I saw a project (Volksforth) working on getting modern Forth working on ancient 8-bit and 16-bit computers. Now that's the sort of dumb thing I wouldn't mind toying with to create a basic videogame on hardware from my past. But it would be easier to convince the lazy part of my brain if there would be any practical outcome from trying to get into this stuff.

Saoshyant fucked around with this message at 01:40 on Aug 19, 2022

Control Volume
Dec 31, 2008

Im trying to learn game design and currently Im brushing up on programming best practices, but Im running into a bit of an understanding wall with dependency injection. Just about every online article describes it as extremely simple, then goes about explaining it in the most convoluted manner possible, so I want to check to see if my understanding is correct on this.

Is dependency injection, in practical effect, just moving an object/variable creation somewhere else and replacing it with an abstracted variable (usually set via input argument) instead?

For example, having something like
code:
void function1() {int x=5; printf(x);}
function1();
and modifying it to:
code:
void function1(x) {printf(x);}
void function2() {int x = 5; return x;}
function1(function2());
would technically be a very basic example of dependency injection, right?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Kind of but not really? While you could look at it as the number 5 being provided as a dependency instead of being hardcoded into the function, it's kind of a farcical extreme, most people would just call that "a function parameter".

When people are talking about DI it's usually in the context of injecting behaviours - instead of hardcoding all the logic, the code calls methods on the dependencies that it's been given, and those methods could do different things depending on the exact implementation that's been given.

As a simple example:
code:

void foo() {
  System.out.println("Hello");
}
vs.
code:

void foo2(OutputStream output) {
  output.println("Hello");
}

Instead of only writing to the system output, how the writing happens and where it gets written to is determined by the dependency that gets passed in.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Control Volume posted:

Im trying to learn game design and currently Im brushing up on programming best practices, but Im running into a bit of an understanding wall with dependency injection. Just about every online article describes it as extremely simple, then goes about explaining it in the most convoluted manner possible, so I want to check to see if my understanding is correct on this.

Is dependency injection, in practical effect, just moving an object/variable creation somewhere else and replacing it with an abstracted variable (usually set via input argument) instead?

For example, having something like
code:
void function1() {int x=5; printf(x);}
function1();
and modifying it to:
code:
void function1(x) {printf(x);}
void function2() {int x = 5; return x;}
function1(function2());
would technically be a very basic example of dependency injection, right?

The way I try to explain DI to juniors is to just use parameterized constructors. If your class needs something, pass that in rather than building it internally. You're then injecting your dependencies.

DI frameworks do things that try to do that for you, such that you no longer do that. Which is where the convolutions and complications come from.

Control Volume
Dec 31, 2008

Jabor posted:

Kind of but not really? While you could look at it as the number 5 being provided as a dependency instead of being hardcoded into the function, it's kind of a farcical extreme, most people would just call that "a function parameter".

When people are talking about DI it's usually in the context of injecting behaviours

Thats the thing Im trying to figure out, if this farcical extreme still technically meets the definition despite being removed from its usual context. None of the articles or videos Ive found use conceptual examples; every single one was in the context of injecting a class object via an interface, but the definition doesnt seem to limit the concept to that.

e: And if the practical definition is "dont call other classes to construct objects, use abstract objects instead" Im fine with that, it just feels way more limiting than the theory suggests.

Control Volume fucked around with this message at 03:25 on Aug 19, 2022

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
I mean, DI is significantly more than "don't call constructors" - it's a whole philosophy about how you structure the logic that goes into your code. You're kinda focusing on the trees and missing the whole forest here.

For example, let's suppose you have an application, and you want to record an analytics event when users hit a particular button, which you'll do by sending a web request to a particular URL.

You might say "oh I'm going to pass the URL to hit into the button, that way I'm doing Dependency Injection", and you kind of are but also kind of aren't doing DI - the button still has all the logic of "when the button gets pressed I'm going to make a web request to a URL". The more DI-ish way to do it would be for the button to accept an AnalyticsLogger as a dependency, and that AnalyticsLogger contains all the logic about how to record an analytics event.

Control Volume
Dec 31, 2008

So its more focused on decoupling processes within the program structure? vs. decoupling in general?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
I don't really understand the question, or what you mean by "decoupling in general". But I think the answer is probably yes.

Control Volume
Dec 31, 2008

Lol Ill go with that, I think you helped me get a better idea of the concept, though, so thanks!

Computer viking
May 30, 2011
Now with less breakage.

The other end of the stick is the practical use:

Consider a Web server - something like apache. When you look at the configuration files, you can choose not just what is logged and how to format it (which presumably ends up as an argument to a log function somewhere), but also where those logs go: Single large text file? Directory full of them, split by topic? Into the operating system's logging system with a configurable tag?

In the same way, apache lets you pick different "engines" - do you have a pool of threads waiting for requests, or a pool of processes? A pool of processes that each have a bunch of threads? Fork a new process to handle each request?

The config files lead to different logging engines and different process handling engines being used, and if their code is written in a reasonable way, this is done through DI: there is a startup procedure that reads the config files and creates the right engines, and then the rest of the code uses them without having to care about which implementation it is.

If you start at this end, the DI "style" makes sense as an answer to "how do I write something that can be configured like that without it being a tangled nightmare".

ExcessBLarg!
Sep 1, 2001

Control Volume posted:

So its more focused on decoupling processes within the program structure? vs. decoupling in general?
I think there's a few layers here:

The main reason you "decouple" something (aside from just the organizational benefit) is if you (might) need to reuse part of the code somewhere else. With the AnalyticsLogger, it makes sense to decouple that from the button because you may also use the AnalyticsLogger on a network event, when a timer fires, things like that.

The main reason you make abstract interfaces is if you (might) need to support different behaviors with different implementations. So you could have a DatabaseLogSink that logs events to a database, a SystemLogSink that logs to your OS's system log, a FileLogSink that logs to a plain text file, all of which are build on the LogSink interface that's used by the AnalyticsLogger object. Furthermore, the AnalyticsLogger itself might implement a generic Logger interface since you could have other kinds of things you want to log aside from analytics--like an ErrorLogger.

Now, when writing your application you may decide you want one AnalyticsLogger, that uses a DatabaseLogSink, and you're passing that as the Logger to your UI events (buttons), and you might also decide that you want an ErrorLogger, with a SystemLogSink, and you're passing that to anything network related. You could create an AnalyticsLogger in the class responsible for building UI objects, and the ErrorLogger in the class responsible for setting up network sockets, and for small applications that's fine, but it's also pretty inflexible.

Alternatively, you could create an AnalyticsLogger at the top-level class of your application and pass that to your UI subsystem constructor, and create/pass an ErrorLogger to your network subsystem, and any objects underneath would need to be aware, and pass along, these loggers. You could even dynamically create these loggers at the top-level by parsing a config file that describes what loggers to use and where they should go. This is manually passing-in dependencies.

The third option is to use a dependency injection framework that allows you to configure your dependencies (often from a config file), and allows consuming objects to specify what dependencies (a Logger) they require, and then when those consuming objected are created the DI framework passes the configured dependencies (the AnalyticsLogger with the DatabaseLogSink) to them. This approach often involves reflection and looks a bit like magic from the outside, but has the benefit that you don't have to manually pass dependencies through your entire object hierarchy.

ExcessBLarg! fucked around with this message at 14:33 on Aug 19, 2022

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Saoshyant posted:

Any of you fellows have any experience in Forth? Like, where is it used these days (practical projects) if at all, if it's worth checking out, how hard it is, etc?

I read a while ago that Forth is the language you learn if you want to know how computers really work and that always intrigued me.

But I didn't think much about it until today when I saw a project (Volksforth) working on getting modern Forth working on ancient 8-bit and 16-bit computers. Now that's the sort of dumb thing I wouldn't mind toying with to create a basic videogame on hardware from my past. But it would be easier to convince the lazy part of my brain if there would be any practical outcome from trying to get into this stuff.

If you're looking for a payoff Forth is not a good choice. Also, at this point there are a lot of layers of abstraction between your code and the hardware that runs it, and I'm not sure that anything will really let you understand what's actually happening.

Computer viking
May 30, 2011
Now with less breakage.

It is probably the best example of a stack-oriented language around, which I guess can be an interesting model to have in mind at times? Still, very niche indeed.

The only Forth I can remember running into in the wild is the FreeBSD boot loader - their equivalent to grub scripts - but I think they rewrote everything to default to Lua instead very recently.

Saoshyant
Oct 26, 2010

:hmmorks: :orks:


ultrafilter posted:

Also, at this point there are a lot of layers of abstraction between your code and the hardware that runs it, and I'm not sure that anything will really let you understand what's actually happening.

Well, that isn't promising.

Computer viking posted:

The only Forth I can remember running into in the wild is the FreeBSD boot loader - their equivalent to grub scripts - but I think they rewrote everything to default to Lua instead very recently.

And that's even less. Guess it's not really solving any problems out there.

Thank you both.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


If you really want to see how programs are physically executed, check out Introduction to Computing Systems: From Bits & Gates to C/C++ & Beyond. It's a fairly standard textbook for a first course in systems and I've heard pretty good things about it. There's a newer edition coming out soon so the 2nd edition will probably be available for cheap, and also it's out there as :filez: if you want to try before you buy.

12 rats tied together
Sep 7, 2006

Control Volume posted:

So its more focused on decoupling processes within the program structure? vs. decoupling in general?

I feel bad for perennially appearing ITT to tell people to watch a video, but you should watch "nothing is something", the Sandi Metz talk from RailsConf 2015. It's a 30 minute watch on youtube, Sandi is a great speaker, and it goes over (in a broad sense): Null Object, Dependency Injection, Composition vs Inheritance, and a couple other things like why naming is hard and how nominative type systems can lead you into traps.

Absolutely worth it, it was a "lightbulb on" moment for me. Everyone else ITT did a good job of explaining, but I think if you're approaching this from a perspective where it hasn't "clicked" yet, the video/talk format is better.

ExcessBLarg!
Sep 1, 2001

Saoshyant posted:

I read a while ago that Forth is the language you learn if you want to know how computers really work and that always intrigued me.
You could make this argument for about a dozen languages and it would be true in entirely different ways.

Forth was a moderately popular language in the 70s and early 80s for being more powerful than BASIC, but still architecture independent (so not assembly) and could still run on an 8-bit microcomputer. These days there's better languages (at least, more modern and familiar) that achieve nearly all the same goals as Forth except perhaps for actually running on an 8-bit micro (but still can run on any modern microcontrollers you'd find in the wild).

Saoshyant posted:

Now that's the sort of dumb thing I wouldn't mind toying with to create a basic videogame on hardware from my past.
Video games tend to fall into either the categories of "simple enough you should write it in BASIC" or "needs to run fast and/or poke hardware enough that it must be written in assembly". The part you'd consider writing in Forth would be scripting of game objects that you want to be architecture independent so you don't have to port the "game logic" part of the game to all the 8-bit platforms you want to run on.

ToxicFrog
Apr 26, 2008


IMO the best reason to learn Forth is that stack languages are fun, and the best reason to implement Forth is that writing compilers is also fun and the ones for stack languages tend to be comparatively straightforward. (Although it turns out writing a self-hosting anything compiler on the AVR is a pain in the rear end because of the Harvard memory architecture :argh:)

I've been poking at Factor lately and it seems pretty neat, like the language I wanted when I was doing inappropriate things with Postscript 20 years ago

bigperm
Jul 10, 2001
some obscure reference
Back in the ancient days (~10 years ago) of minecraft modding there was a mod that had a computer that you could program in Forth (or 6502 ASM). Later mods switched to Lua.

ExcessBLarg!
Sep 1, 2001
Everyone loves Lua now and I'm sure it's "fine", more fine than JavaScript anyways. I just don't trust languages that use the same data structure for indexed and associative arrays.

Computer viking
May 30, 2011
Now with less breakage.

ExcessBLarg! posted:

Everyone loves Lua now and I'm sure it's "fine", more fine than JavaScript anyways. I just don't trust languages that use the same data structure for indexed and associative arrays.

Ha, thats an extremely specific quibble, but I'm not sure if I disagree.

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!
Trying to write a custom program/script for Windows 10 that will send a keystroke to a specific minimized window at an interval without foregrounding said window. I can’t use AHK.

Never developed anything for/on a Windows PC before so I’m confused. What do I need to get this done? Language/API/dev env wise?

Boris Galerkin fucked around with this message at 17:26 on Aug 22, 2022

raminasi
Jan 25, 2005

a last drink with no ice

Computer viking posted:

It is probably the best example of a stack-oriented language around, which I guess can be an interesting model to have in mind at times? Still, very niche indeed.

The only Forth I can remember running into in the wild is the FreeBSD boot loader - their equivalent to grub scripts - but I think they rewrote everything to default to Lua instead very recently.

.NET IL is stack-oriented. I actually wonder how many people can write IL by hand compared to the number of Forth-writers that exist.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Boris Galerkin posted:

Trying to write a custom program/script for Windows 10 that will send a keystroke to a specific minimized window at an interval without foregrounding said window. I can’t use AHK.

Never developed anything for/on a Windows PC before so I’m confused. What do I need to get this done? Language/API/dev env wise?

You can probably do that by sending a windows message. Don't take that as gospel, but a quick google suggests it's true. https://stackoverflow.com/questions/2113950/how-to-send-keystrokes-to-a-window

But as to how you should approach the problem - it would be easiest if you gave some indication of what kind of technology you'd prefer to do it in (.NET, C++, scripting language, other...)

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Boris Galerkin posted:

Trying to write a custom program/script for Windows 10 that will send a keystroke to a specific minimized window at an interval without foregrounding said window. I can’t use AHK.

Never developed anything for/on a Windows PC before so I’m confused. What do I need to get this done? Language/API/dev env wise?

Not to suggest this isn't possible, but this has the smell of an X/Y problem. What are you actually doing here?

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!

Volmarias posted:

Not to suggest this isn't possible, but this has the smell of an X/Y problem. What are you actually doing here?

I want to macro a boring thing in a video game. Literally just need to have it press “f” every 10 seconds, something like that.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Boris Galerkin posted:

I want to macro a boring thing in a video game. Literally just need to have it press “f” every 10 seconds, something like that.

Who are you paying respects to?

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!

Hughmoris posted:

Who are you paying respects to?

My integrity, I guess.

E: I don’t know anything about development environments/languages for Windows. But if I can use C++ or Python that would be great.

E: https://stackoverflow.com/questions/13564851/how-to-generate-keyboard-events I think this and the other SO link above gives me enough information to get started.

Boris Galerkin fucked around with this message at 20:08 on Aug 22, 2022

bobmarleysghost
Mar 7, 2006



Is this the right place for a regex/awk question?

I want to filter out any lines that match "root" from a list or processes.

The list is generated through:
code:
ps -eo pid,etime,user,comm
code:
21153 3-23:58:19 user1 udt
21980 3-09:16:52 root in.telnetd
21981 3-09:16:52 root login
21982 3-09:16:50 user1 bash
22004 3-09:16:50 user3 bash
23401 3-12:04:53 root in.telnetd
23402 3-12:04:53 root login
23403 3-12:04:51 user2 bash
23425 3-12:04:51 user3 bash
28071 3-23:28:47 root in.telnetd
28072 3-23:28:47 root login
28073 3-23:28:45 user1 bash
I then pipe the output to awk with the criteria - field 2 must start with a "3-" and field 3 must not include the word "root":
code:
 | awk '$2~/^[3]-/ && $3~/^(?:(?!root).)*/ { print $1, $2, $3, $4 }'
The problem is that when I run the commands I still see the lines matching "root" in the output.
If for example I change the field 3 regex to "only look for root", I get only lines that include "root". But I can't get the opposite to work.

The regex works on its own but not in awk, what am I doing wrong?

nielsm
Jun 1, 2009



Have you tried $3 !~ /^root/ ?

bobmarleysghost
Mar 7, 2006



omg

bobmarleysghost
Mar 7, 2006



That completely missed me. I was sure it had to be complicated.

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Boris Galerkin posted:

I want to macro a boring thing in a video game. Literally just need to have it press “f” every 10 seconds, something like that.

Note that if this is an MMO or something like it, this sort of pattern is not at all unlikely to get detected and get your account banned.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply