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
huhu
Feb 24, 2006

Peristalsis posted:

6 to 12 months

I only have a 6 month contract so there's that lol.

I survived the week by continually asking for 4-8 hour chunks of concrete tasks and it's working out more or less.

Adbot
ADBOT LOVES YOU

thehustler
Apr 17, 2004

I am very curious about this little crescendo
I'm writing some CTF binaries for friends to solve and I'm having an annoying issue. My binaries, after disabling ASLR, get loaded into a very low address. Those zeroes effectively kill any kind of overflow I can do because strings will be terminated.

Can I change where my compiled code gets loaded?

csammis
Aug 26, 2003

Mental Institution
If you're using Visual Studio then I think you want /BASE

e: and check the value of /FIXED

thehustler
Apr 17, 2004

I am very curious about this little crescendo
Linux I'm afraid

taqueso
Mar 8, 2004


:911:
:wookie: :thermidor: :wookie:
:dehumanize:

:pirate::hf::tinfoil:

You should be able to change that with a linker script maybe this will help

LP0 ON FIRE
Jan 25, 2006

beep boop
I'm running perl scripts in bash with an argument like:

myPerlScript.pl arg1

Then retrieving the arguments in perl like:

(my $myVar) = @ARGV;

1) How do I properly attach multiple arguments when calling the perl script from bash?

2) How I retrieve multiple arguments in the perl script?

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

LP0 ON FIRE posted:

1) How do I properly attach multiple arguments when calling the perl script from bash?

2) How I retrieve multiple arguments in the perl script?

code:
perl -e 'use strict; my ($foo, $bar, $baz) = @ARGV; print "$foo, $bar, $baz\n"' a b c

LP0 ON FIRE
Jan 25, 2006

beep boop

TooMuchAbstraction posted:

code:
perl -e 'use strict; my ($foo, $bar, $baz) = @ARGV; print "$foo, $bar, $baz\n"' a b c

Sorry I'm really confused, because that looks like a perl command of code, not calling a file attaching arguments. And what is the a b c?

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

LP0 ON FIRE posted:

Sorry I'm really confused, because that looks like a perl command of code, not calling a file attaching arguments. And what is the a b c?

"perl -e" invokes Perl on the command line, executing whatever is in the following string. The string is a Perl command, which, prettified, looks like this:

code:
use strict;
my ($foo, $bar, $baz) = @ARGV;
print "$foo, $bar, $baz\n";
The "a b c" then are the commandline arguments to this code, so they get stuffed into @ARGV when it is executed.

In short, to pass multiple arguments, just list them in your invocation:

myPerlScript.pl arg1 arg2 arg3

And use the "my ($arg1, $arg2, $arg3) = @ARGV" syntax to access them in the script.

Perl's greatest strength is its utility on the commandline, so it's worth becoming familiar with the -e flag and writing Perl one-liners. You can use it to process lines of text, for example. Indeed, this is such a common pattern:
code:
% perl -e 'foreach <STDIN>) {chomp; doSomethingWith $_}'
that they introduced the -n flag to feed each line to the script individually:
code:
% perl -ne 'chomp; doSomethingWith $_'

LP0 ON FIRE
Jan 25, 2006

beep boop

TooMuchAbstraction posted:

arguments, just list them in your invocation:

myPerlScript.pl arg1 arg2 arg3


That's actually what I've tried, and then I tried foreach in the perl script like:

code:
foreach my $updateVar (@ARGV){

    print $updateVar;

}

Which only prints the first argument passed.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
You're gonna have to give more context on how you're invoking your Perl script, then, because something's going wrong there.

LP0 ON FIRE
Jan 25, 2006

beep boop

TooMuchAbstraction posted:

You're gonna have to give more context on how you're invoking your Perl script, then, because something's going wrong there.

Good point. I'm using the saveAndRun extension in vs code in the workspace settings:

code:
{
    "saveAndRun": {

        "commands": [
            {
                "match": "dashboard.php",
                "cmd": "incCachedVersion.pl cacheVersion_dashboard cacheVersion_conference"
            },
            {
                "match": "instructors.php",
                "cmd": "includes/preUserAuthOperations/incCachedVersion.pl cacheVersion_instructors"
            },
            {
                "match": "newInstructorAccounts.php",
                "cmd": "incCachedVersion.pl cacheVersion_instructors"
            },
            {
                "match": "conferenceJS.php",
                "cmd": "incCachedVersion.pl cacheVersion_conference"
            }
        ]

    }

}
So when I save any of these files, it runs the perl scripts. The first one in the list is the one I'm trying to use two arguments with.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

LP0 ON FIRE posted:

Good point. I'm using the saveAndRun extension in vs code in the workspace settings:

So when I save any of these files, it runs the perl scripts. The first one in the list is the one I'm trying to use two arguments with.

Ah, sorry, I have zero familiarity with VS Code. :(

fankey
Aug 31, 2001

I'm trying to write a c++ app which uses libcurl to grab stuff via the github api. It works perfectly fine ( verbose output ) using the curl exe ( ignore the [url], forums put that in.. )
code:
curl -v [url]https://api.github.com/repos/curl/curl/readme[/url]
but with my code
code:
#include <winsock2.h>
#include <WS2tcpip.h>
#include <curl/curl.h>
#include <iostream>

int main(int argc, char** argv)
{
  WORD  wVersionRequested = MAKEWORD(2, 2);
  WSADATA wsaData;
  WSAStartup(wVersionRequested, &wsaData);
  if (argc > 1)
  {
    char error_buffer[256];
    error_buffer[0] = 0;
    CURL* easy = curl_easy_init();
    curl_easy_setopt(easy, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_easy_setopt(easy, CURLOPT_SSL_ENABLE_ALPN, 1);
    curl_easy_setopt(easy, CURLOPT_URL, argv[1]);
    curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, 4000);
    curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1);
    curl_easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(easy, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(easy, CURLOPT_SSL_VERIFYHOST, 0);
    curl_easy_setopt(easy, CURLOPT_ERRORBUFFER, error_buffer);
    CURLcode res = curl_easy_perform(easy);
    if (res != CURLE_OK)
    {
      std::cout << error_buffer << std::endl;
      std::cout << "curl_easy_perform " << curl_easy_strerror(res) << std::endl;
    }
  }
}
I get at 403 ( verbose output ). I assume I'm missing a particular CURLOPT but haven't been able to figure it out. Other https sites work fine with my test code so it's something particular with github.

ate shit on live tv
Feb 15, 2004

by Azathoth
I'm learning C# and trying to do some windows graphics programming. I have a python background, but basically zero graphical experience. I have some old Java experience so I have a very basic understanding of what methods are, but I'm confused as to how you use them or turn them into the equivalent of a python function.

Take this code:

code:
private async void Sound(object sender, RoutedEventArgs e)
        {
            MediaElement mediaElement = new MediaElement();
            var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
            Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World!");
            mediaElement.SetSource(stream, stream.ContentType);
            mediaElement.Play();
        }
What that does is when you hit a button called "Sound" in Mainpage.xaml it calls the windows text-to-speech thing and says "Hello World." I don't full understand how that works yet, but it does work. Now I want to pass a string to that "Sound object."

Like so:
code:
var_string = "Hello World"
private async void Sound(object sender, RoutedEventArgs e)
   {
       PlaySound(var_string);
   }
// Or Better Yet
//Some code etc
button.sound(var_string); //Should play whatever is contained in the variable "var_string" when the button called "Sound" is pressed.
//some more code
Here is a similar thing, this code creates an ellipse on the canvas.

code:
        void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            args.DrawingSession.DrawEllipse(155, 115, 80, 30, Colors.Black, 3);
        }
But what if I want to turn that into a function that's just called DrawEllipse(var_w, var_x, var_y, var_z, var_color, var_width), then call that when I hit a button, basically like this:

code:
private async void DrawIt(object sender, RoutedEventArgs e)
   {
        DrawEllipse(var_w, var_x, var_y, var_z, var_color, var_width);
   }
I'm sure this is super basic functionality, but every time I try to google methods or functions, I get an explanation that assumes I already know how to create a function or a method, but I don't have these basic building blocks yet. I don't even know what the purpose of a "namespace" is, or why I'd use one, or how to create a different one, etc.

A c# visual studio 2017 tutorial would be nice, but all the ones I've found are out of date and I can't get much of the sample code to work usually since it involves console.write, or system.drawing, which either doesn't exist anymore, or I can't use for reasons I don't understand.

ate shit on live tv fucked around with this message at 01:33 on May 2, 2017

LP0 ON FIRE
Jan 25, 2006

beep boop

TooMuchAbstraction posted:

Ah, sorry, I have zero familiarity with VS Code. :(

It's all good. I probably just need to have a more involved line like the -e flag and such. Those cmd settings are supposed to be just like a bash command line. I'll experiment more tomorrow. Thanks.

raminasi
Jan 25, 2005

a last drink with no ice

ate poo poo on live tv posted:

I'm learning C# and trying to do some windows graphics programming. I have a python background, but basically zero graphical experience. I have some old Java experience so I have a very basic understanding of what methods are, but I'm confused as to how you use them or turn them into the equivalent of a python function.

Take this code:

code:
private async void Sound(object sender, RoutedEventArgs e)
        {
            MediaElement mediaElement = new MediaElement();
            var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
            Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World!");
            mediaElement.SetSource(stream, stream.ContentType);
            mediaElement.Play();
        }
What that does is when you hit a button called "Sound" in Mainpage.xaml it calls the windows text-to-speech thing and says "Hello World." I don't full understand how that works yet, but it does work. Now I want to pass a string to that "Sound object."

Like so:
code:
var_string = "Hello World"
private async void Sound(object sender, RoutedEventArgs e)
   {
       PlaySound(var_string);
   }
// Or Better Yet
//Some code etc
button.sound(var_string); //Should play whatever is contained in the variable "var_string" when the button called "Sound" is pressed.
//some more code
Here is a similar thing, this code creates an ellipse on the canvas.

code:
        void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            args.DrawingSession.DrawEllipse(155, 115, 80, 30, Colors.Black, 3);
        }
But what if I want to turn that into a function that's just called DrawEllipse(var_w, var_x, var_y, var_z, var_color, var_width), then call that when I hit a button, basically like this:

code:
private async void DrawIt(object sender, RoutedEventArgs e)
   {
        DrawEllipse(var_w, var_x, var_y, var_z, var_color, var_width);
   }
I'm sure this is super basic functionality, but every time I try to google methods or functions, I get an explanation that assumes I already know how to create a function or a method, but I don't have these basic building blocks yet. I don't even know what the purpose of a "namespace" is, or why I'd use one, or how to create a different one, etc.

A c# visual studio 2017 tutorial would be nice, but all the ones I've found are out of date and I can't get much of the sample code to work usually since it involves console.write, or system.drawing, which either doesn't exist anymore, or I can't use for reasons I don't understand.

I've heard good things about this series of video tutorials, assuming you're the kind of person who can learn from videos. (I'm not.) It's for absolute beginners, so it might be pretty slow for you, but it will cover all the fundamental stuff that's getting glossed over.

The examples you've given here have some more advanced stuff thrown into the mix, so I'd advise forgetting about them.

LP0 ON FIRE
Jan 25, 2006

beep boop
Hey TooMuchAbstraction I figured out my command line bash problem. I didn't post the whole perl script, because I expect that wouldn't be the problem, but I found out from stackoverflow question I stumbled upon, that the open file handler needs to reinstated every time the loop runs: http://stackoverflow.com/questions/3086576/nested-foreach-loop-in-perl-only-looping-once

Works now :)

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
Awesome, glad to hear you got it working!

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
I've been writing mostly server-side Java code for the past 8 years, with a little bit of Angular web-clients in the past 2. I've just been put on a project writing an end-client C# agent that needs to interact with the Windows event log. What kinds of things do I not realize that I don't know about writing end-client code? Are there any good resources that I should check out?

uncle blog
Nov 18, 2012

This isn't really a programming question, but I figured this still might be the best place to ask it. I'm currently making an app for people studying urban planning, and I want a snazzy background on some of the screens. So I wondered if anyone knew about places with resources that can be used freely for such purposes. Specifically I want something like an overhead view of a stylized urban environment. Tried mocking up something myself, but I'm not super at stuff like that.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

uncle blog posted:

This isn't really a programming question, but I figured this still might be the best place to ask it. I'm currently making an app for people studying urban planning, and I want a snazzy background on some of the screens. So I wondered if anyone knew about places with resources that can be used freely for such purposes. Specifically I want something like an overhead view of a stylized urban environment. Tried mocking up something myself, but I'm not super at stuff like that.

Go play Cities: Skylines for a bit and take a screenshot? :shrug:

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Gravity Pike posted:

I've been writing mostly server-side Java code for the past 8 years, with a little bit of Angular web-clients in the past 2. I've just been put on a project writing an end-client C# agent that needs to interact with the Windows event log. What kinds of things do I not realize that I don't know about writing end-client code? Are there any good resources that I should check out?

Is there a UI or is it a pure service?

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Munkeymon posted:

Is there a UI or is it a pure service?

Pure service. There's a menu-bar thing that needs to basically display the version and advertise that it is, in fact, running.

raminasi
Jan 25, 2005

a last drink with no ice

uncle blog posted:

This isn't really a programming question, but I figured this still might be the best place to ask it. I'm currently making an app for people studying urban planning, and I want a snazzy background on some of the screens. So I wondered if anyone knew about places with resources that can be used freely for such purposes. Specifically I want something like an overhead view of a stylized urban environment. Tried mocking up something myself, but I'm not super at stuff like that.

Are you associated with academics? That's exactly the kind of scut work that professors can usually squeeze out of urban design students, if you've got access to them.

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

raminasi posted:

Are you associated with academics? That's exactly the kind of scut work that professors can usually squeeze out of urban design students, if you've got access to them.

This is true in every field.

Also, Stock Exchange.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Gravity Pike posted:

Pure service. There's a menu-bar thing that needs to basically display the version and advertise that it is, in fact, running.

Then you probably don't need to worry about details like UI thread vs main thread, which was my first thought. You might not know that a registered service, which is how I'm assuming you'd run your agent, can't have any user-visible UI at all (see http://stackoverflow.com/a/2652285/301807)? Relatedly, you'll want to write it such that the functional bits are walled off in their own library so you can debug it in a console application and distribute it with a service application. Other than that, you might want to ask in the .Net thread :)

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Does anyone know of a database diagramming tool* compatible with SQL Server other/better than the one that comes with SQL Server Manager? Particularly, something I could configure to recognise some relationships that aren't, say, officially represented by nice, sane foreign keys, which is one problem with using the MS tool. The other being that it's really bad at coming up with a sane layout on its own.

*as in, examine schema + maybe a config, emit diagram, not a drag and drop table designer

kiwid
Sep 30, 2013

Is PluralSight a good training resource?

I need to pick something for my work to pay for and I've seen PluralSight recommended in a few places.

I'm looking for general IT hardware/software, programming (javascript, python), and data analysis/business intelligence stuff.

I haven't seen the data/business stuff on any other training sights I've looked at.

If not PluralSight, what should I be looking at?

Also, it could be a free resource but I'm not opposed to making my employer fork the bill for something better.

The Fool
Oct 16, 2003


kiwid posted:

Is PluralSight a good training resource?

I need to pick something for my work to pay for and I've seen PluralSight recommended in a few places.

I'm looking for general IT hardware/software, programming (javascript, python), and data analysis/business intelligence stuff.

I haven't seen the data/business stuff on any other training sights I've looked at.

If not PluralSight, what should I be looking at?

Also, it could be a free resource but I'm not opposed to making my employer fork the bill for something better.

In my experience PluralSight seems better for programming stuff, CBT Nuggets seems better for IT stuff.

I also like the O'Reilly Safari library.

huhu
Feb 24, 2006

kiwid posted:

Is PluralSight a good training resource?

I need to pick something for my work to pay for and I've seen PluralSight recommended in a few places.

I'm looking for general IT hardware/software, programming (javascript, python), and data analysis/business intelligence stuff.

I haven't seen the data/business stuff on any other training sights I've looked at.

If not PluralSight, what should I be looking at?

Also, it could be a free resource but I'm not opposed to making my employer fork the bill for something better.

I've used Lynda and Pluaralsight to learn. Pluaralsight is good because it gives you paths which are basically guides for how to progress through a given tool or language. Lynda is much less structured. However, if you're supposed to learn a given subject and the instructor sucks, you don't have a choice with Pluaralsight unless you look on YouTube and find a better alternative. Pluaralsight also is weird with the paths because some of the courses for a given path are kind of like why is this here.

I'm kind of just rambling. Let me know if you have specific questions.

kiwid
Sep 30, 2013

The Fool posted:

In my experience PluralSight seems better for programming stuff, CBT Nuggets seems better for IT stuff.

I also like the O'Reilly Safari library.

Never heard of O'Reilly Safari. I'll check it out.

huhu posted:

I've used Lynda and Pluaralsight to learn. Pluaralsight is good because it gives you paths which are basically guides for how to progress through a given tool or language. Lynda is much less structured. However, if you're supposed to learn a given subject and the instructor sucks, you don't have a choice with Pluaralsight unless you look on YouTube and find a better alternative. Pluaralsight also is weird with the paths because some of the courses for a given path are kind of like why is this here.

I'm kind of just rambling. Let me know if you have specific questions.

It looks like Lynda is a better resource than it was 2 years ago when I tried it. Maybe I'll get them to buy both.

Has anyone used KnowledgeNet before?

Mopp
Oct 29, 2004

I'm crossposting this from the infosec thread:

I have a problem with some data that's encoded in an unknown way. The decoded data is most likely human readable text.

A hexdump shows that it the ASCII representation of the binary data is hex. Could someone help me figure out what magic this is? I've been guessing that it might be a protobuf or similar protocol, but haven't been able to reverse anything.

code:
00000000  30 30 30 30 31 31 30 30  30 35 30 30 37 38 64 61  |00001100050078da|
00000010  32 62 34 61 34 64 34 63  61 39 30 34 30 30 30 36  |2b4a4d4ca9040006|
00000020  33 37 30 32 31 36                                 |370216|

Backhand
Sep 25, 2008
So, question. I'm an intermediate-level .NET and SQL guy who wants to continue getting to be less poo poo at programming. Obviously training is really the only way to do this, but like everybody else time is a factor for me. The one time of day that's basically a dead zone for me is my commute, which amounts to probably 50 minutes round-trip spent in the car every day. This has also led to me discovering that audiobooks are a pretty great way to pass the time if you're regularly spending decent amounts of time in a car. (The Martian loving rocks; I loved the movie, and it turns out the book makes it look like poo poo by comparison.)

So, just throwing this out there: Are there any audiobooks that do a decent job passing along programming knowledge in a format that doesn't require 100% concentration? I tried the Programming Throwdown and Herding Code podcasts for a while, but ultimately didn't find them too useful; they take an extremely long time to pass along information that's ultimately so high-level it's nearly useless. My hope is there's some other source I can listen in on that will allow me to make something productive of my drive, but I'm guessing I'll be told when I already suspect: That studying programming poo poo just requires too much concentration and focus and visual learning to be done realistically in a car via pure audio.

Still, it's worth a shot.... anybody had any luck with programming audiobooks or podcasts during longer drives?

Peristalsis
Apr 5, 2004
Move along.

Backhand posted:

So, question. I'm an intermediate-level .NET and SQL guy who wants to continue getting to be less poo poo at programming. Obviously training is really the only way to do this, but like everybody else time is a factor for me. The one time of day that's basically a dead zone for me is my commute, which amounts to probably 50 minutes round-trip spent in the car every day. This has also led to me discovering that audiobooks are a pretty great way to pass the time if you're regularly spending decent amounts of time in a car. (The Martian loving rocks; I loved the movie, and it turns out the book makes it look like poo poo by comparison.)

So, just throwing this out there: Are there any audiobooks that do a decent job passing along programming knowledge in a format that doesn't require 100% concentration? I tried the Programming Throwdown and Herding Code podcasts for a while, but ultimately didn't find them too useful; they take an extremely long time to pass along information that's ultimately so high-level it's nearly useless. My hope is there's some other source I can listen in on that will allow me to make something productive of my drive, but I'm guessing I'll be told when I already suspect: That studying programming poo poo just requires too much concentration and focus and visual learning to be done realistically in a car via pure audio.

Still, it's worth a shot.... anybody had any luck with programming audiobooks or podcasts during longer drives?

Can you take a bus or train to work?

I'd never be able to really learn anything while driving, but I'm able to take a bus for most of my commute, and I've made a habit of reading real books while en route. It's NOT ideal - putting 20-25 minutes at a time into a dense computer science book makes the book drag out for a long time, and I never seem to get around to typing the exercises I write out in my spiral notebook into an actual interpreter/compiler to debug, but it's better than nothing, which is what I'd likely be doing otherwise.

B-Nasty
May 25, 2005

Backhand posted:

Still, it's worth a shot.... anybody had any luck with programming audiobooks or podcasts during longer drives?

As you concluded, I don't think really learning programming concepts works without directly reading and writing code. I do, however, listen to podcasts to get some high-level exposure to new technologies and concepts that I then investigate when I'm at a computer. Frankly, what makes a senior-level software engineer in my opinion is being able to digest all the information that is out there and make decisions regarding direction and architecture. The implementation is usually the easier, more straight-forward side of things.

Anyway, I like (.NET related):
-Dot Not Rocks Podcast (annoying hosts, but good guests)
-Coding Blocks Podcast
-Scott Hanselman's Podcast

Triglav
Jun 2, 2007

IT IS HARAAM TO SEND SMILEY FACES THROUGH THE INTERNET
So I have two arrays, one with things I'm interested in (needles) and another taken from a constantly changing API (haystack). The contents and length of the haystack are constantly changing.

I'm trying to quickly iterate over the haystack and find values associated with my needles, but the way I'm doing it is way too slow for my liking. In pseudocode, this is what I'm doing:

code:
needles = ["b","c"]
haystack = ["a"=>7,"b"=>4,"b"=>3,"c"=>11]

foreach needles as needle
  foreach haystack as key=>val
    if needle==key
      print val
    end
  end
end
It works, but it's way too slow. It repeatedly iterates through the haystack for each needle. If the haystack were tiny I probably wouldn't care much, but it's large. How can I do things faster?

Steve French
Sep 8, 2003

How many "needles" are there?

redleader
Aug 18, 2005

Engage according to operational parameters

Triglav posted:

So I have two arrays, one with things I'm interested in (needles) and another taken from a constantly changing API (haystack). The contents and length of the haystack are constantly changing.

I'm trying to quickly iterate over the haystack and find values associated with my needles, but the way I'm doing it is way too slow for my liking. In pseudocode, this is what I'm doing:

code:
needles = ["b","c"]
haystack = ["a"=>7,"b"=>4,"b"=>3,"c"=>11]

foreach needles as needle
  foreach haystack as key=>val
    if needle==key
      print val
    end
  end
end
It works, but it's way too slow. It repeatedly iterates through the haystack for each needle. If the haystack were tiny I probably wouldn't care much, but it's large. How can I do things faster?

Turn haystack into a hashmap for quick lookups.

code:
haystackMap = { key: value for (key, value) in haystack }    // Create hashmap from haystack

foreach needle in needles:
    if haystackMap.hasKey(needle):
        print haystackMap[needle]    // Gets they key's value
end
This needs to loop over haystack once. Looking up a value in a hashmap is a constant-time operation, so looking for the needles should be noticeably faster. You'll need to figure out the details of creating and looking up values from a hashmap for the language you're using, since every language has their own unique snowflake differences.

Other questions to ponder:
  • Have you profiled your code and found that this is a bottleneck?
  • How big is haystack? I'm a little surprised that the time from this operation is noticeable at all.

Adbot
ADBOT LOVES YOU

Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.
Without a definition of "constantly changing" it's hard to say, but yeah as the poster above said, hash table.

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