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
JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

StumblyWumbly posted:

Hey, I'm a hard/firmware guy who only writes in C, Verilog and VHDL.

I need a simple language I can use to write scripts to manipulate text files. About the most complicated thing I'll do is read in a text file, strip out some stuff, and convert the ASCII into hex.

What language or tool would be best for this kind of stuff? It would have to run on a Windows XP system.

Python.

Some will say Perl, but Perl is terrible. Others will say Ruby, which is a better choice, but I think Python will fit your background better.

Adbot
ADBOT LOVES YOU

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

JoeNotCharles posted:

Python.

Some will say Perl, but Perl is terrible. Others will say Ruby, which is a better choice, but I think Python will fit your background better.

I don't it's fair to dismiss a language as terrible without any explanation. In fact you didn't even mention why ruby or python are any better than Perl at text processing which has always been Perl's forte.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

MEAT TREAT posted:

I don't it's fair to dismiss a language as terrible without any explanation. In fact you didn't even mention why ruby or python are any better than Perl at text processing which has always been Perl's forte.

It may not be "fair", but it's accurate. Perl is a terrible language, and I would strongly advise everyone to avoid as hard as possible.

Ruby and Python aren't better than Perl at text processing - Ruby is just as good (because it stole all its text handling from Perl, so it's pretty much the same in that respect), and has the advantage of not being terrible in all other respects. Python is slightly more cumbersome, but not enough to be a huge problem, and will be easier to pick up for somebody who only knows C because it doesn't have a lot of new control-flow constructs to learn.

hey mom its 420
May 12, 2007

Yeah, go either with Python, Perl or Ruby. Check out some code examples to see which syntax you like the best. Their text parsing capabilities I'd say are on par, although Ruby and Perl have regexes build right into the syntax, but Python is good with regexes too.
Also I think Python would have a minimum learning curve. Also Ruby's main thing is a lot of stuff you probably won't really need for text parsing, like closures and metaprogramming. And Perl is known for not being able to read your own code after 5 minutes of writing it.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Uhh, whatever. If you want the job done in a blindingly fast manner with respect to time invested/development speed, go Perl, absolutely. If these go beyond "one time use" then either 1) learn how to program good, defensive Perl 2) :( choose a different language.

Perl is tits at processing text. Let no one tell you otherwise. How the code looks is completely different from how it got it done. And it gets it done quickly and sexily. Well, in that retarded, gruesome, camel kinda way.

tef
May 30, 2004

-> some l-system crap ->
Chiming in with the whole "You can write bad unreadable code in any language" point.


I would recommend the shell if you were on a unix machine, as it is perfectly capable of small text processing tasks.

Do what bonus says and go and look at some examples and pick one. Really the advantages and disavantages between the choices of ruby, perl and python are trivial at best for the tasks you want.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Triple Tech posted:

Uhh, whatever. If you want the job done in a blindingly fast manner with respect to time invested/development speed, go Perl, absolutely.

Ruby is better than Perl for this.

quote:

If these go beyond "one time use" then either 1) learn how to program good, defensive Perl 2) :( choose a different language.

Everything goes beyond a one time use.

Learning to program "good, defensive Perl" is a huge undertaking compared to learning how to program well in any other language. The language structure is just set up to throw everything together and lash it up with whatever's handy. Sure, some people have the discipline to write intricate, well-designed code in Perl, but if you're just starting out so you have nothing invested in Perl, why would you go to the effort?

JawnV6
Jul 4, 2004

So hot ...

6174 posted:

TAOCP is The Art of Computer Programming by Knuth.

edit: Since posting in the first place I've read (but not personally verified) that there is some discussion about hashing in the Dragon book as well (Compilers: Principles Techniques and Tools by Aho et al).
I'll look into those two, does CLRS have any clear discussion on hashes? I've got that on my shelf at home, not quite sure why I didn't check it in the first place.

StumblyWumbly posted:

Hey, I'm a hard/firmware guy who only writes in C, Verilog and VHDL.
I came from a hardware/C background and took to perl very easily. We also have a lot of legacy perl lying around, perl API's abound, much more relevant than Ruby or Python would have been. Again, your results may vary.

6174
Dec 4, 2004

JawnV6 posted:

I'll look into those two, does CLRS have any clear discussion on hashes? I've got that on my shelf at home, not quite sure why I didn't check it in the first place.

CLRS has some discussion according to the table of contents.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

edit: ughh wrong thread

Acer Pilot fucked around with this message at 10:41 on Apr 3, 2008

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
I'd just like to point out that Perl has changed in the last decade, so please stop getting your Perl code from Matt's Script Archive.

It'd also be nice if people would discuss programming without getting into language debates..

StumblyWumbly: I'd recommend any of Perl, Python, or Ruby. I know Perl can run fine on Windows, but I don't know about the other two. Look up ActiveState Perl or Strawberry Perl.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
I forgot to mention that one reason I specifically recommended Python was that I know it works extremely well on Windows (I actually prefer the Windows installer from python.org to ActiveState Python, which has add-ons).

I also was in too much of a hurry earlier to say the reason I recommend it to someone who knows C - a lot of the built-in libraries it comes with are organized like common POSIX libraries, so it should be quite familiar to someone who mainly knows C.

floWenoL
Oct 23, 2002

JoeNotCharles posted:

It may not be "fair", but it's accurate. Perl is a terrible language, and I would strongly advise everyone to avoid as hard as possible.

Why don't you try making recommendations without sparking a language debate? Even though your rose-colored (or ruby-colored ha ha) evaluation of Python and Ruby's text processing capabilities as compared to Perl's are amusing.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

floWenoL posted:

Why don't you try making recommendations without sparking a language debate? Even though your rose-colored (or ruby-colored ha ha) evaluation of Python and Ruby's text processing capabilities as compared to Perl's are amusing.

Because the guy specifically asked for text-processing languages, so I knew somebody very early on would say, "Perl is the no-brainer for this," which it is not.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

JoeNotCharles posted:

Because the guy specifically asked for text-processing languages, so I knew somebody very early on would say, "Perl is the no-brainer for this," which it is not.

Why don't you spell out for us what text-processing tasks Perl is so terribly unsuited for that Python and Ruby spank it at?

tef
May 30, 2004

-> some l-system crap ->
Why don't you two take this outside and settle this like real programmers ?

csammis
Aug 26, 2003

Mental Institution
I mean seriously it's not like we have Perl or Python or Ruby threads so people can decide for themselves what they should use :rolleye:

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

tef posted:

Why don't you two take this outside and settle this like real programmers ?
Whoah, outside? What kind of language-warring programmer goes out there?

covener
Jan 10, 2004

You know, for kids!

Mustach posted:

Whoah, outside? What kind of language-warring programmer goes out there?

Obligatory jargon file link: The Big Room

Trebuchet King
Jul 5, 2005

This post...

...is a
WORK OF FICTION!!



Matlab question: I've written up a program to perform multiple linear regression, and instead of handling numbers in normal, decimal format, it spits them out like 1/3450*9402285^(1/2). My first thought was that I'd jut forgotten to undo a format rat after the last lab class, but no matter how many times I enter format, format short g, or anything else, it stays in that form, which fprintf statements don't seem to like. How the heck do I bully it into using plain ol' decimal form?

I've had a couple other weird issues (errors when it tries to print, errors when putting a legend on a plot) so I'm thinking I might just need to reinstall, but I figured it would be best to check first.

Scaevolus
Apr 16, 2007

Is there a name for the algorithm to convert a sorted list to a balanced binary search tree (something more efficient than iterating over the list and inserting items into an AVL tree)? I think I can do it in O(n), but Knuth probably already created a better algorithm 30 years ago or something.

I have a crappy illustration of what I mean (the left hand side of the diagram, I've already figured out what f(x) is)

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Scaevolus posted:

Is there a name for the algorithm to convert a sorted list to a balanced binary search tree (something more efficient than iterating over the list and inserting items into an AVL tree)? I think I can do it in O(n), but Knuth probably already created a better algorithm 30 years ago or something.

Uhm... a sorted array is a balanced binary tree. The top node is the index closest to the middle, which you can get in constant time as long as you know the highest index, the left subtree is all indices before it, and the right subtree is all indices after it.

Incoherence
May 22, 2004

POYO AND TEAR

ShoulderDaemon posted:

Uhm... a sorted array is a balanced binary tree. The top node is the index closest to the middle, which you can get in constant time as long as you know the highest index, the left subtree is all indices before it, and the right subtree is all indices after it.
Sounds like he wants to start with a sorted array and make a balanced binary tree so that he can add more crap to it (with O(lg n) insertion instead of O(n)).

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Incoherence posted:

Sounds like he wants to start with a sorted array and make a balanced binary tree so that he can add more crap to it (with O(lg n) insertion instead of O(n)).

Well then O(n) is a trivially provable lower bound, because he needs one access per node in order to create the complete tree (although he could construct a lazy tree).

To build a new tree you'd just walk the old tree in preorder and build the new tree with a balanced assumption.

code:
#include <assert.h>
#include <stdlib.h>

struct bitree {
  int           value;
  struct bitree *left;
  struct bitree *right;
};

struct bitree *array_to_bitree ( const int *array, unsigned int length ) {

  if ( length == 0 )
    return NULL;

  struct bitree *root;

  root = malloc( sizeof( struct bitree ) );
  assert( root );

  root->value = array[length / 2];

  root->left  = array_to_bitree( array, length / 2 );
  root->right = array_to_bitree( array + length / 2 + 1, length - (length / 2 + 1) );

  return root;

}
(completely untested)

Scaevolus
Apr 16, 2007

Sorry, let me try to explain better.

I don't need to modify this tree after I've created it.
By balanced binary search tree, I mean a balanced left-heavy tree, that is, the number of nodes to the right is <= the number of nodes to the left.

Basically, I want to flatten this binary tree in an array, where I know the position of the children nodes based only on the index of the parent node. In my example, I'm transforming {1,2,3,4,5,6} to {4,2,6,1,3,5}.

The question is if this process has a name I can use to refer to it easily?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Scaevolus posted:

Sorry, let me try to explain better.

I don't need to modify this tree after I've created it.
By balanced binary search tree, I mean a balanced left-heavy tree, that is, the number of nodes to the right is <= the number of nodes to the left.

Basically, I want to flatten this binary tree in an array, where I know the position of the children nodes based only on the index of the parent node. In my example, I'm transforming {1,2,3,4,5,6} to {4,2,6,1,3,5}.

The question is if this process has a name I can use to refer to it easily?

No name that I'm aware of.

But again, if you already have a sorted array, then you already have such a tree available. In this case, you'd have to tweak the code I provided so that it enforces the left-heavy nature of the tree:

code:
struct bitree *array_to_bitree ( const int *array, unsigned int length ) {

  if ( length == 0 )
    return NULL;

  struct bitree *root;

  // For left-heavy balanced trees, the head is always at index:
  // 2^(dl2 - 1) + min( 2^(dl2 - 1) - 1, length - 2^dl2 )
  // Where dl2 is the discrete log base 2 of the length.
  // Simplification of the expression is left as an exercise to the reader.

  int dl2 = 0;
  for ( unsigned int templength = length; templength > 0; templength >>= 1 )
    dl2++;

  unsigned int head = (1 << (dl2 - 1))
                    + min( (1 << (dl2 - 1)) - 1, length - (1 << dl2) );

  root = malloc( sizeof( struct bitree ) );
  assert( root );

  root->value = array[head];

  root->left  = array_to_bitree( array, head );
  root->right = array_to_bitree( array + head + 1, length - (head + 1) );

  return root;

}
If you don't want to construct the pointers, then obviously you can just iterate the index discovery expression used here.

Scaevolus
Apr 16, 2007

ShoulderDaemon posted:

If you don't want to construct the pointers, then obviously you can just iterate the index discovery expression used here.
Each node will only have 12 bytes of data, so it's pretty big if I can save 8 bytes on pointers.

Never mind, I was just wondering if there was a name for it.

nbv4
Aug 21, 2002

by Duchess Gummybuns
I have a form on a webpage that has like 50 checkboxes. I'm using a javascript function to validate the form. I'm trying to formulate an IF statement that returns true if there are more than one of those boxes checked. I'm thinking the XOR operator would be best in this situation, but apparently javascript doesn't have a xor operator... This is what I'm kind of wanting to do:

code:
if(checkbox1 ^^ checkbox2 ^^ checkbox3 ^^ checkbox4 ... )
    alert("you can't check more than one box");
and no, using radio buttons instead is not a proper solution.

I found this page, which talks about this issue, but it quickly degrades into moon language with exclamation marks and carrots everywhere. Whats the cleanest way to do this?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

nbv4 posted:

I have a form on a webpage that has like 50 checkboxes. I'm using a javascript function to validate the form. I'm trying to formulate an IF statement that returns true if there are more than one of those boxes checked. I'm thinking the XOR operator would be best in this situation,

OK, no. XOR will tell you the parity of your checkboxes, but it won't tell you if more than one was checked.

If the variables checkbox1, checkbox2, etc. are all either 1 or 0, then just add them together and verify that the result is exactly 1.

Incoherence
May 22, 2004

POYO AND TEAR

nbv4 posted:

and no, using radio buttons instead is not a proper solution.
Find your UI designer and stab them. If it's you, stab yourself.

But back to your original question: logical-XORing a bunch of poo poo together just tells you whether an odd number of them are true. Easiest way to find out if more than one is true is just to assign a value (0, 1) to each and add them up.

hey mom its 420
May 12, 2007

I am currently running GHCi on Windows, the version is 6.8.2 and I seem to be missing some functions that I find around in various Haskell references. For example when I try using nub, sort, or groupBy it says that they're not in scope. It looks like about 25% of functions are missing. Am I using an older version or do I have to import some modules for those functions to work or what?

tef
May 30, 2004

-> some l-system crap ->

nbv4 posted:

I have a form on a webpage that has like 50 checkboxes. I'm using a javascript function to validate the form. I'm trying to formulate an IF statement that returns true if there are more than one of those boxes checked.

Put an on change in each checkbox - when it is set add one to a total, and when it is unset substract one. The form is valid if the total is greater than one?

10011
Jul 22, 2007

Bonus posted:

I am currently running GHCi on Windows, the version is 6.8.2 and I seem to be missing some functions that I find around in various Haskell references. For example when I try using nub, sort, or groupBy it says that they're not in scope. It looks like about 25% of functions are missing. Am I using an older version or do I have to import some modules for those functions to work or what?

The three functions you mentioned are all in Data.List. To import modules in GHCi you use :m +<name>.

hey wiz
Jun 18, 2005

tef posted:

Put an on change in each checkbox - when it is set add one to a total, and when it is unset substract one. The form is valid if the total is greater than one?
If he used an on change event, why not deselect all other check boxes before selecting the current one?

poopiehead
Oct 6, 2004

hey wiz posted:

If he used an on change event, why not deselect all other check boxes before selecting the current one?

Along the same lines.... Why a checkbox and not a radio box?

no need to write code to enforce rules that the browser will enforce for free

Mystery Wizard
Nov 24, 2007
To anyone who knows Javascript - would you respond and let me know that you actually know what you are talking about.

I'm looking for some answers to help me with my web page.

-C

noonches
Dec 5, 2005

It represents my lifestyle and stratus as a street savy irreverent youth, who lives large, and yet hungers for the next level in life.
Why don't you just post your question here, or in this thread, I'm sure there's plenty of people who actually know javascript.

-N

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

hey wiz posted:

If he used an on change event, why not deselect all other check boxes before selecting the current one?

Because that's really confusing for the user.

hey wiz
Jun 18, 2005

Plastic Jesus posted:

Because that's really confusing for the user.
Agreed, but using checkboxes and shoddy logic to implement the functionality of radio buttons is in no way less confusing.

Adbot
ADBOT LOVES YOU

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

nbv4 posted:

and no, using radio buttons instead is not a proper solution.

Bullshit.

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