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
Sneftel
Jan 28, 2009
Ideally you'd just set the regex to non-greedy, but if you can't do that, do (\[[^\]]*\]). That is, an opening square bracket, followed by zero or more not-a-closing-square-brackets, followed by a closing square bracket.

Adbot
ADBOT LOVES YOU

nielsm
Jun 1, 2009



SnakePlissken posted:

(\[.*\]) is greedy, gets all hits at once. And (\[.*\])? doesn't work. All the quick documentation I've found say to use a ? after, and it's not working.

I think what you're looking for would be the expression .*?, with *? being the non-greedy zero-or-more.

Alternatively, make a negative character class: (\[[^\]]*\])
"A [, followed by a number of characters that are not ], followed by a ]."
(fe: drat beaten)

SnakePlissken
Dec 31, 2009

by zen death robot

Sneftel posted:

Ideally you'd just set the regex to non-greedy, but if you can't do that, do (\[[^\]]*\]). That is, an opening square bracket, followed by zero or more not-a-closing-square-brackets, followed by a closing square bracket.

By George, that did it! I came close to pulling hair on this one the other night, so thanks!

SnakePlissken
Dec 31, 2009

by zen death robot

nielsm posted:

I think what you're looking for would be the expression .*?, with *? being the non-greedy zero-or-more.

Alternatively, make a negative character class: (\[[^\]]*\])
"A [, followed by a number of characters that are not ], followed by a ]."
(fe: drat beaten)

Thanks to you too! Your answer is instructive.

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
I'm trying to brush up on C# efficiency/optimisation in certain situations, but struggling to find a definitive answer for one particular scenario.

Let's say you have a method which takes a paramlist of any type:
code:
public void MyStrangeMethod(params object[] paramList)
{
   [...]
}
At some point you're going to need to iterate over the array and do some casting, so the aim is to keep casts to a minimum. Let's say we're passing strings at the moment, then the following only involves one cast so is the best you can achieve:

code:
foreach (object o in paramList)
{
   string s = o as string;
   if (s != null)
   {
      //do stuff with strings
   }
}
But what happens if you're passing both ints and strings? You can't do int i = o as int because int isn't nullable, which leaves you with:

Option 1:
code:
foreach (object o in paramList)
{
   int? i = o as int?;
   if (i != null)
   {
      //do stuff with ints, but now your ints are nullable where they weren't before
   }
}
Option 2:
code:
foreach (object o in paramList)
{
   if (o.GetType().Equals(typeof(int)))
   {
      int i = (int)o;
      //do stuff with ints which are still 'normal' ints
   }
}
So my question is, how many casts are involved in option 2? Obviously there's the explicit one inside the if block, but is the if expression really doing casts behind the scenes to perform the comparison?

Thanks, and sorry if this was a bit long.

rolleyes fucked around with this message at 14:20 on Feb 13, 2011

Milotic
Mar 4, 2009

9CL apologist
Slippery Tilde

rolleyes posted:

Thanks, and sorry if this was a bit long.

The compiler will most likely have to do automatic boxing around the int in order to be able to call GetType() on it. ints are primitive in .NET, so it will be boxed to the Integer class.

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?

Milotic posted:

The compiler will most likely have to do automatic boxing around the int in order to be able to call GetType() on it. ints are primitive in .NET, so it will be boxed to the Integer class.

Unless I'm confusing myself with terminology, aren't ints in an object[] array boxed already? If the compiler needs to unbox the ints to do GetType() then that answer my question, as that's a cast (i.e. (int)o).

If that's true then there is no ideal solution; option 1 uses one cast but leaves you with nullable ints, while option 2 preserves non-nullable ints but uses two casts.

rolleyes fucked around with this message at 16:08 on Feb 13, 2011

ninjeff
Jan 19, 2004

rolleyes posted:

So my question is, how many casts are involved in option 2? Obviously there's the explicit one inside the if block, but is the if expression really doing casts behind the scenes to perform the comparison?

Thanks, and sorry if this was a bit long.

I believe you'll get two casts in both cases, but what you could try is:
code:
foreach (int i in paramList.OfType<int>)
{
    // do stuff
}
I think that will let you only do one cast per list element, and it's clearer to boot!

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?

ninjeff posted:

I believe you'll get two casts in both cases, but what you could try is:
code:
foreach (int i in paramList.OfType<int>)
{
    // do stuff
}
I think that will let you only do one cast per list element, and it's clearer to boot!

Hmm, interesting. First I forgot System.Linq, but after sorting that out I get "foreach statement cannot operate on variables of type 'method group' because 'method group' does not contain a public definition for 'GetEnumerator'".

Also, could you expand on why you think option 1 would produce two casts? The "as" operator is a single cast according to MSDN.

Orzo
Sep 3, 2004

IT! IT is confusing! Say your goddamn pronouns!
It's a method, you need () after OfType<int>

Also, why are you worrying so much about the number of casts that your application is performing? Is it a design or performance issue?

ninjeff
Jan 19, 2004

rolleyes posted:

Hmm, interesting. First I forgot System.Linq, but after sorting that out I get "foreach statement cannot operate on variables of type 'method group' because 'method group' does not contain a public definition for 'GetEnumerator'".
Whoops, there I go forgetting C# syntax again - I confess I mainly code in VB. Orzo's post above has the answer.

quote:

Also, could you expand on why you think option 1 would produce two casts? The "as" operator is a single cast according to MSDN.
You're right, that's only one cast. The LINQ solution still gets around the nullability issue, though. :)

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
Doh, I should have spotted the missing parentheses myself! That does now compile.

Orzo: I don't have an application as such, I'm just trying to expand my optimisation knowledge a bit and hit this question while doing so. I know casts are expensive so it makes sense to reduce them especially when they're in a loop, and I was wondering what the 'ideal' solution to this particular scenario is; assuming ninjeff's method is just one cast then that appears to be it.

In short, just curiosity. :)

Jose Cuervo
Aug 25, 2004
I have a program that creates graphs which runs from the command line. The command
code:
gle -d pdf -o graph1 graphCode.gle arg1 arg2
creates graph1.pdf where arg1 and arg2 are two arguments unique to graph1.pdf. I have to create 30 graphs with differing arg1 and arg2 values, and I was wondering if there was a way for me to write a file with each line containing a command with the arg1 and arg2 values as appropriate, such that when I run that single file, it executes all the commands and creates all the graphs automatically.

Is there a way to do this?

I am using Win7 if that makes a difference.

Grem
Mar 29, 2004
Probation
Can't post for 28 days!
I hope I'm asking in the right place. This is the first website I've ever worked on: http://mizsweetgurl.aisites.com/redoabout.html

Some links refuse to point to the correct location. For instance, the about link on that page doesn't point to http://mizsweetgurl.aisites.com/redoabout.html, instead it points to http://mizsweetgurl.aisites.com/redo%20about.html. I think the space then fucks up the link and males them not work. I'm using Adobe Dreamweaver. What am I doing wrong here? I went back through every page on Dreamweaver and made sure it said in the source that all the links pointed to the correct destination.

nielsm
Jun 1, 2009



Jose Cuervo posted:

I have a program that creates graphs which runs from the command line. The command
code:
gle -d pdf -o graph1 graphCode.gle arg1 arg2
creates graph1.pdf where arg1 and arg2 are two arguments unique to graph1.pdf. I have to create 30 graphs with differing arg1 and arg2 values, and I was wondering if there was a way for me to write a file with each line containing a command with the arg1 and arg2 values as appropriate, such that when I run that single file, it executes all the commands and creates all the graphs automatically.

Is there a way to do this?

I am using Win7 if that makes a difference.

Windows NT batch files can do crazy stuff with horrible syntax.

makegraphs.bat (or just enter it in a cmd window)
code:
FOR /F "tokens=1,2,3" %i IN (graphlist.txt) DO gle -d pdf -o %i graphCode.gle %j %k
graphlist.txt
pre:
graph1 arg1 arg2
graph2 arg3 arg4
graph3 arg5 arg6
I haven't tested this.

Jose Cuervo
Aug 25, 2004
The batch files idea worked perfectly. I now just have to double click the batch file and I have 30 different graphs 10 seconds later. Thank you.

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
I'm back with my casts again, because I got around to adding some benchmarks. The results are not quite what was expected. I've timed all 3 options using a stopwatch instance (we'll call ninjeff's method "Option 3") iterating over a 1,000 element array where the odd numbers are ints and the even numbers are strings. The only work being done is to pull out the ints from the array and add each one to a running total in the foreach loop. The output consistently gives results like this:

Option 1 took 00:00:00.0002734
Option 2 took 00:00:00.0000430
Option 3 took 00:00:00.0048696


Option 1: I thought this would be quicker as it only uses one cast, but it's in 2nd place.
Option 2: I wasn't sure about this one as I wasn't certain if it used one cast or two. From these results I'm guessing o.GetType().Equals(typeof(int)) doesn't involve a cast, and maybe the performance gain is from using normal non-nullable ints.
Option 3: LINQ appears to ruin performance as this is two orders of magnitude slower than Option 2.

The more you know! :science:

rolleyes fucked around with this message at 11:10 on Feb 17, 2011

baquerd
Jul 2, 2007

by FactsAreUseless

rolleyes posted:

Option 1 took 00:00:00.0002734
Option 2 took 00:00:00.0000430
Option 3 took 00:00:00.0048696


These aren't reliable, you need more data and more data sets.

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?

baquerd posted:

These aren't reliable, you need more data and more data sets.

Fair point. I haven't changed the composition but I've increased the size of the array to 10,000, and I'm also now running each option 10,000 times and taking the average:

Option 1 avg/10,000: 00:00:00.0004072
Option 2 avg/10,000: 00:00:00.0000723
Option 3 avg/10,000: 00:00:00.0002582


So Option 1 and Option 3 have traded places and the difference between is much reduced, but Option 2 still wins by a wide margin.


edit:
Just tried it with the composition changed so the first 5,000 items in the array are ints and the second 5,000 are strings. No appreciable difference in the results.

rolleyes fucked around with this message at 13:23 on Feb 17, 2011

baquerd
Jul 2, 2007

by FactsAreUseless

rolleyes posted:

Fair point. I haven't changed the composition but I've increased the size of the array to 10,000, and I'm also now running each option 10,000 times and taking the average:

Just tried it with the composition changed so the first 5,000 items in the array are ints and the second 5,000 are strings. No appreciable difference in the results.

What about very large Strings? What about a million very large Strings? What if you run a program in the background too? What if you There are a great many considerations for profiling, and "ran a few tests, looks good" doesn't meet very robust standards.

That said, your result is correct that number 2 is going to be vastly more efficient.

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
If this was something which was actually going into a performance sensitive environment then I'd agree with you, but as mentioned above this is just my own little curiosity project.

In this particular setup the length of the string shouldn't matter as the strings are skipped - the only processing (other than the "are you an int?" test) happens on the ints.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



I'm writing a ParseKit grammar (looks like Extended Backus-Naur Form), but I'm having problems with this production:

code:
datePeriod	= 'FROM' dateSimple |
		  'TO' dateSimple | 
		  'FROM' dateSimple 'TO' dateSimple;
When parsing a string such as "FROM (date) TO (date)", I get issued callbacks for the various parts of the production, but in seemingly random order. So for example, I get a callback with an assembly of the tokens (FROM, dateSimple), then I get (FROM, dateSimple, TO, dateSimple).

This naturally wreaks havoc on my state if one of the partials come before the full assembly.

I've sorta fixed it by updating my entry production to match & discard brackets and wrapping the string I want to parse in the same brackets, but this seems like a giant hack. Any suggestions?

Carthag Tuek fucked around with this message at 20:33 on Feb 17, 2011

csammis
Aug 26, 2003

Mental Institution
I'm not familiar with ParseKit but that production looks really ambiguous to me. Is this parser going to find longest match? Because if not then given the input

code:
FIND ALL dates FROM date TO date
at index 15 it'll match two kinds of datePeriod: the production 'FROM dateSimple' and the production 'FROM dateSimple TO dateSimple'. If it selects the first then it will go on to match 'TO dateSimple' but if it selects the second it'll take the entire sequence as a single production, and this indecision is probably what you're seeing.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Yeah that's the problem exactly. For reference, my grammar is basically the GEDCOM grammar reformatted to ParseKit:

code:
	 DATE_PERIOD: = {Size=7:35} 
	 [ 
	 FROM <DATE> | 
	 TO <DATE> | 
	 FROM <DATE> TO <DATE> 
	 ] 
I can't see anywhere in the ParseKit docs that I can force it to always choose the longest match :(

Edit: Btw, Gedcom is a horrible horrible broken standard, but as it's de facto there's not much to do at the moment.

Carthag Tuek fucked around with this message at 20:42 on Feb 17, 2011

csammis
Aug 26, 2003

Mental Institution
Yeesh, genealogy. What about something like this:

code:
DATE_PERIOD := 'FROM' <DATE_PERIOD_FROM> |
               'TO' <DATE>

DATE_PERIOD_FROM := <DATE> |
                    <DATE> 'TO' <DATE>
The parser will observe a FROM token and either take a single date or the compound. I think this works with GEDCOM's example text...

quote:

Examples:
FROM 1904 to 1915
=The state of some attribute existed from 1904 to 1915 inclusive.
FROM 1904
=The state of the attribute began in 1904 but the end date is unknown.
TO 1915
=The state ended in 1915 but the begin date is unknown.
...but it's been a while since I worked with grammars of any complexity at all :shobon:

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



That's basically the same thing as far as I can see. It's still ambiguous which part of DATE_PERIOD_FROM will trigger. Just to be sure, I tried it in the grammar, and I got the same error again.

Thanks though. I'll try and gently caress around with it some more tomorrow, but as long as the bracket hack works, it's good enough I guess.

csammis
Aug 26, 2003

Mental Institution
Ah, yup. Durrrrrr.

Um...maybe you can list the longest production first in your grammar?

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Doesn't appear to work, and frankly I'm tired of grammars & parsing. I've been working on the drat thing since last night.

Only reason I'm using a grammar at all is because the dates are so complex. The full lines I tokenize with two regular expressions (plus a third to capture nonstanrd tags) and it's working just fine.

Carthag Tuek fucked around with this message at 22:27 on Feb 17, 2011

Geno
Apr 26, 2004
STUPID
DICK
i need to learn Tcl/Tk for some scripting at work. anyone have any recommended books/websites?

Sneftel
Jan 28, 2009

Geno posted:

i need to learn Tcl/Tk for some scripting at work. anyone have any recommended books/websites?

monster.com? :cool:

Effective Tcl/Tk is a good guide for people who are already programmers. O'Reilly has a good Tcl/Tk reference, but it's not good for learning.

Seashell Salesman
Aug 4, 2005

Holy wow! That "Literally A Person" sure is a cool and good poster. He's smart and witty and he smells like a pure mountain stream. I posted in his thread and I got a FANCY NEW AVATAR!!!!
This is probably a stupid question but I've searched online and not turned up anything conclusive. I do all my work in formal methods these days so whenever I run into a real programming problem I usually can't remember what to do.

I'm making a little toy program that has a few AI controlled objects move around on a map (so they need to detect collisions and whatnot). The objects need to know the shape of the map class, but the map in turn would have to know the shape of the object class (so that it can provide some method to detect if there is an object blocking a map tile, for instance).

Now that's pretty straight forward, but my problem is it is a circular dependency. From what I can gather, non-braindead GC can handle circular references just fine, and non-braindead compilers can handle bidirectional or circular dependencies. Assuming that last part is true (and correct me if I'm wrong) is it still morally wrong set up classes like this?

The only alternate structure that occurs to me is to have some intermediary that has both the map and object information, but then if objects are given their own opportunity to act, they have to call the intermediary which brings the circular dependency back.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Seashell Salesman posted:

is it still morally wrong set up classes [with a circular dependency]?

What's the problem you're trying to solve here? It sounds like you're looking for a problem where there isn't one. Do you have a scenario in mind where your circular dependency causes problems?

With garbage collectors that can't detect cycles, you often find weak references. Normally references are strong, meaning they prevent the GC from collecting the referred-to object. A weak reference allows collection to happen. Basically the GC will collect objects that have zero strong references to them, regardless of how many weak references there are. So your map object might have a strong reference to the other objects, but the other objects just have a weak reference back to the map.

For compilers, you'll see forward declarations that say "there is some class named map, but don't worry about it right now". This is common in C thanks to splitting the interface (header file) and implementation combined with a stupid preprocessor that would otherwise get in a loop, even though the compiler can figure it out just fine.

Maybe you know all this already and I'm wasting your time, but I figured we could start there. And for what it's worth, morality has nothing to do with it!

Seashell Salesman
Aug 4, 2005

Holy wow! That "Literally A Person" sure is a cool and good poster. He's smart and witty and he smells like a pure mountain stream. I posted in his thread and I got a FANCY NEW AVATAR!!!!

pokeyman posted:

What's the problem you're trying to solve here? It sounds like you're looking for a problem where there isn't one. Do you have a scenario in mind where your circular dependency causes problems?

With garbage collectors that can't detect cycles, you often find weak references. Normally references are strong, meaning they prevent the GC from collecting the referred-to object. A weak reference allows collection to happen. Basically the GC will collect objects that have zero strong references to them, regardless of how many weak references there are. So your map object might have a strong reference to the other objects, but the other objects just have a weak reference back to the map.

For compilers, you'll see forward declarations that say "there is some class named map, but don't worry about it right now". This is common in C thanks to splitting the interface (header file) and implementation combined with a stupid preprocessor that would otherwise get in a loop, even though the compiler can figure it out just fine.

Maybe you know all this already and I'm wasting your time, but I figured we could start there. And for what it's worth, morality has nothing to do with it!

It's relieving to hear I'm mostly right not to worry about (those particular) implementation issues :)

My problem is more or less "is this a coherent structure and is this terrible style for maintenance/reasoning about execution"

Secx
Mar 1, 2003


Hippopotamus retardus
XPath/Nokogiri question.

I just started using Ruby and Nokogiri to extract some information from HTML files. Here's a simplified version of the information I am trying to extract:

<td id="j_id785:resultTable:1:j_id834">Name 1</td>
<td id="j_id785:resultTable:2:j_id834">Name 2</td>
<td id="j_id785:resultTable:3:j_id834">Name 3</td>
...

I am using

doc.search('//td[@id="j_id785:resultTable:1:j_id834"]')

to extract Name 1. I'm a real newbie with regards to Nokogiri. Is there some sort of wildcard I can use so that it matches something like j_id785:resultTable:*:j_id834?

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



You might be able to work something with starts-with() or contains() -- like the below (untested):

//td[starts-with(@id,'j_id785:resultTable')]

FoiledAgain
May 6, 2007

I have a question about asking questions: I'm looking for help with artificial neural networks. Does this count as programming and is this a good forum? Or is there a better place to post?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Fire away and see what happens!

shrughes
Oct 11, 2008

(call/cc call/cc)
Yeah it does and yeah this is a pretty good forum to ask.

Barney Must Die
Jul 21, 2008

Dog Crashes Car
Nothing is more awesome than a car crashing dog
Here's hoping someone might be able to help my with a little vbscript issue I'm having, keep in mind I'm relatively new to vbscript but have done programming in the past. The vbscript is being used within SecureCRT 5.2.1

Here's the code section:

code:
#$language = "VBScript.Encode"
#$interface = "1.0"

'**Start Encode**

' Constants for setting MessageBox options... 
'
Const ICON_STOP = 16		' Critical message; displays STOP icon.
Const ICON_QUESTION = 32	' Warning query; displays '?' icon.
Const ICON_WARN = 48		' Warning message; displays '!' icon.
Const ICON_INFO= 64		' Information message; displays 'i' icon.

Const BUTTON_OK = 0		' OK button only
Const BUTTON_CANCEL = 1		' OK and Cancel buttons
Const BUTTON_ABORTRETRYIGNORE = 2	' Abort, Retry, and Ignore buttons
Const BUTTON_YESNOCANCEL = 3	' Yes, No, and Cancel buttons
Const BUTTON_YESNO = 4		' Yes and No buttons
Const BUTTON_RETRYCANCEL = 5	' Retry and Cancel buttons

Const DEFBUTTON1 = 0	' First button is default
Const DEFBUTTON2 = 256	' Second button is default
Const DEFBUTTON3 = 512 	' Third button is default

' Possible return values of the MessageBox function...
'
Const IDOK = 1		' OK button clicked
Const IDCANCEL = 2	' Cancel button clicked
Const IDABORT =  3	' Abort button clicked
Const IDRETRY =  4	' Retry button clicked
Const IDIGNORE = 5	' Ignore button clicked
Const IDYES = 6		' Yes button clicked
Const IDNO = 7		' No button clicked

Sub Main()

   Dim

   Set objSysInfo = CreateObject( "WinNTSystemInfo" )
   strUserName = objSysInfo.UserName
   Set objFSO = CreateObject("Scripting.FileSystemObject")

   menuselect = crt.Dialog.Prompt("Main Menu" & VbCrLf & VbCrLf & "1 - Option 1" & VbCrLf & "2 - Option 2", "Options", "1")
   If (menuselect = IDCANCEL) Then
      MsgBox "You hit cancel"
   Elseif (menuselect = "") Then
      MsgBox "You entered null"
   Else
      MsgBox "You entered some characters"
   End If

End Sub
My problem is that hitting cancel acts the same as sending a null (ie. zero character entry), but I need to be able to accept a null as an entry. I can comment out the "Const IDCANCEL = 2" line and then null is accepted but the cancel button no longer works.

I've attempted to use an InputBox as well but am running into the same problem.

Is it possible to either change what the code uses as the cancel (ie. not make it a null)?
Or is it possible in a text input box to remove the cancel button altogether?

Adbot
ADBOT LOVES YOU

Goofankle
Feb 4, 2010
would you mind taking a look, please?


this is the error I get.

http://i51.tinypic.com/156uuis.png

code:
//xxxxxxxxxxxx
//xxxxxxxxxxxx
//P4.5






/**
   A DataSet computes the total and average value of a collection of numbers.
*/
/**
  * 	@author 
  *
   */



public class DataSet
{



   private int total;
   private int count;




   /**
      Constructs an empty data set.
   */

   public DataSet()
   {
      total = 0;
      count = 0;
   }




  /**
      Add the values
      @param x the value to be added
   */

   public void addValue(int x)
   {
      count++;
      total = total + x;
   }




   /**
      Computes the sum of the values.
      @return the sum of the values
   */

   public int getSum()
   {
      return total;

   }



  /**
      Computes the average of the values.
      @return the average of the values
   */

  public double getAverage()
   {
      return (double) (total) / count;

   }





}

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