• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 00:48
CEST 06:48
KST 13:48
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL22] Finals Preview: Sweet Victory!9[ASL22] Ro4 Preview: Mirror Mirror6[ASL22] Ro8 Preview: Within Reach5[ASL22] Ro8 Preview: In A Tizzy11[ASL22] Ro16 Preview: Holy Diver5
Community News
ISSL (IntoTheTV x SOOP SC2 League): Premier3Weekly Cups (October 12-18): 5.0.17 era begins1[$1250] International ShieldBattery League50Weekly Cups (October 5-11): Farewell to 5.0.164Rollback Netcode in Brood War145
StarCraft 2
General
How do you feel about the mass reverts in the 5.0.17 PTR? No more SC2 streamers on team liquid? Weekly Cups (October 12-18): 5.0.17 era begins Server Blocker SCII needs to put more weight on strategy and tact
Tourneys
ScienceCraft (October 24-25) - Live Event ISSL (IntoTheTV x SOOP SC2 League): Premier WORTEX Challenger Series — Open SC2 LAN in Budapes RSL goes to London! 2026 Offline Finals Nov 21-22 Sparkling Tuna Cup - Weekly Open Tournament
Strategy
[H] ZvP Mid-Late Game: Stalkers Collossi HT
Custom Maps
[G] How to playtest arcade maps [A] Nemrods 1/4 players [M] (2) Sweltering Sands [M] (2) Frigid Storage
External Content
Mutation # 548 Shining Bright The PondCast: SC2 News & Results Mutation # 547 Temple Of Terror Mutation # 546 Catch the Train
Brood War
General
Do we even care about balance? BW General Discussion SOOP Announces New Women's Monthly Tournament [Megathread] Daily Funny Videos Rollback Netcode in Brood War
Tourneys
[$1250] International ShieldBattery League [ASL22] Grand Finals [Women's StarCraft] Tier 8 CK 2026-10-20 [Megathread] Daily Proleagues
Strategy
Cliff Jump Revisited (1 in a 1000 strategy) Simple Questions, Simple Answers Replay Review Process - What do you do?
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Armies of Exigo - YesYes? Dawn of War IV Warcraft III: The Frozen Throne
Dota 2
Dota 2 Champions League Season 3 Begins April 25!
League of Legends
[TL LoL EUW IHs] Teemo shall perish
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Canadian Politics Mega-thread The Chess Thread Things Aren’t Peaceful in Palestine Artificial Intelligence Thread
Fan Clubs
Serral Fan Club
Media & Entertainment
Movie Discussion! [Manga] One Piece
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Recent Gifted Posts
Blogs
How Much Genes Contribute to…
TrAiDoS
ShieldBattery on Mac
Kraekkling
[ASL22] Ro4 Day1 Ticket Giv…
bITt.mAN
38 yo Retired SWE loo…
PurE)Rabbit-SF
Can Bots Beat Pros?? Starcr…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 8372 users

The Big Programming Thread - Page 156

Forum Index > General Forum
Post a Reply
Prev 1 154 155 156 157 158 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Dephy
Profile Joined January 2011
Lithuania163 Posts
Last Edited: 2012-08-10 10:19:42
August 10 2012 10:19 GMT
#3101
i wanted to ask how to make stream link appear and dissapear from the list depeding if its on or off, like in TL? is there some short workaround? it shouldnt be hard?
EscPlan9
Profile Blog Joined December 2006
United States2777 Posts
August 12 2012 04:22 GMT
#3102
Without using any modules in Perl, I need to create a script that recursively traverses the directory and then output the total size of all the files and a few other statistics like the last modified file, etc. I have the recursion part somewhat down correctly in that everytime a directory is found the subroutine is called again to traverse that next directory, but the problem is it doesn't backtrack to the parent directories.

The pertinent section is below:

sub scanDir {
my $curDir = shift;

chdir($curDir) or die "Unable to change to dir $curDir: $!\n";
opendir DIR, "." or die "Unable to open $curDir: $!\n";
my @contents = readdir(DIR) or die "Unable to read $curDir: $!\n";
closedir(DIR);

for (@contents) {
# ignore the "." and ".." listings
next if ($_ eq "." || $_ eq "..");

if (-d "$_") {
evaluateFile($_);
print "$_ is a dir of size " . $fileSize . " descending into...\n";
scanDir($_);
}
elsif (-f "$_") {
evaluateFile($_);
print "$_ is a file, size = " . $fileSize . "\n";
}
}
}


I'm trying to think of ways to make sure it runs through every subdirectory without any repetitions. The problem right now is once it finds a directory, it jumps into it and scans all the files and directories there, and it keeps doing that until no more directories are found. This means that in a directory with multiple subdirectories, only the first subdirectory is traversed through. It never returns through the other subdirectories.

From google searches I always see that people recommend using modules for this. My teacher wants us to tackle this problem without using modules though.

One possible method I can think of is creating a hash structure of Absolute-path-of-dir : Boolean (traversed or not). I would create new keys when new directories are found and update the values for the directories that I have traversed until all the keys -> values evaluate to True. While I think this might work, it'll be tricky and I feel like there must be a much simpler method I am missing. Any ideas?
Undefeated TL Tecmo Super Bowl League Champion
supereddie
Profile Joined March 2011
Netherlands151 Posts
August 12 2012 08:07 GMT
#3103
$_ is a global, so you'll probably run into weird stuff if you depend too much on it. Also, $fileSize seems to be global as well. Why don't you use it as a return from evaluateFile? Lastly, do you want the total size of a directory including subdirectorys? In that case you'll want to return the total size as well in scanDir.
"Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design
EscPlan9
Profile Blog Joined December 2006
United States2777 Posts
August 12 2012 14:11 GMT
#3104
Yeah I cut off some of the code (like the evaluateFile subroutine). Yes, $fileSize is a global. You're right I can just use it as a return from evaluateFile. And finally, yes, the size of the directory and subdirectories themselves must be included.

I'm not sure what you mean by $_ is a global here. If I understand Perl's scope correctly, $_ scope is only within the for loop and gets assigned the value of the current element iterated through within the @contents array (listing of the directory).
Undefeated TL Tecmo Super Bowl League Champion
EscPlan9
Profile Blog Joined December 2006
United States2777 Posts
Last Edited: 2012-08-12 15:09:25
August 12 2012 14:43 GMT
#3105
I figured it out. As I was thinking about possible problems with $_ (thanks for the suggestions supereddie), I decided to try passing the absolute path as the parameter this time rather than just the current path I was in (although I have only found out how to grab the absolute path by using the CWD module in Perl). I also took out the chdir line.

The changed portion:

sub scanDir {
my $curDir = shift;

opendir DIR, $curDir or die "Unable to open $curDir: $!\n";
my @contents = readdir(DIR) or die "Unable to read $curDir: $!\n";
closedir(DIR);

for (@contents) {
# ignore the "." and ".." listings
next if ($_ eq "." || $_ eq "..");

if (-d "$curDir/$_") {
my $abs_path = abs_path("$curDir/$_");
my $dirSize = evaluateFile("$abs_path");
print "$abs_path" . " is a dir of size " . $dirSize . " descending into...\n";
scanDir("$abs_path");
}
elsif (-f "$curDir/$_") {
my $fileSize = evaluateFile("$curDir/$_");
print "$_" . " is a file, size = " . $fileSize . "\n";
}
}
}


And if curious on the other subroutines and how it all comes together, full script in spoiler
+ Show Spoiler +


#!/usr/bin/perl
# dirwalk.pl

use strict;
use warnings;
use Cwd 'abs_path'; # module for Current Working Directory, evaluates absolute path

my $totalSize = 0;
my $lastModName = "";
my $lastModTime = 0;

# update totalSize, check for last modified and update if necessary
sub evaluateFile {
my $fileName = $_[0];
my @info = stat($fileName);
my $bytes = $info[7];
$totalSize += $bytes;

my $epochModTime = $info[9];
if ($epochModTime > $lastModTime) {
$lastModTime = $epochModTime;
$lastModName = substr($fileName, rindex($fileName,"/")+1);
}

return $bytes;
}

# open the directory and scan all the contents
sub scanDir {
my $curDir = shift;

opendir DIR, $curDir or die "Unable to open $curDir: $!\n";
my @contents = readdir(DIR) or die "Unable to read $curDir: $!\n";
closedir(DIR);

for (@contents) {
# ignore the "." and ".." listings
next if ($_ eq "." || $_ eq "..");

if (-d "$curDir/$_") {
my $abs_path = abs_path("$curDir/$_");
my $dirSize = evaluateFile("$abs_path");
print "$abs_path" . " is a dir of size " . $dirSize . " descending into...\n";
scanDir("$abs_path");
}
elsif (-f "$curDir/$_") {
my $fileSize = evaluateFile("$curDir/$_");
print "$_" . " is a file, size = " . $fileSize . "\n";
}
}
}

# validate parameter is a readable dir and only 1 argument passed
if ( !defined($ARGV[0]) || scalar @ARGV > 1 ) {
print STDERR "Enter a directory\n";
exit 1;
} elsif ( (!(-d $ARGV[0])) || (!(-r $ARGV[0])) ) {
print STDERR "Enter a readable directory\n";
exit 1;
}

scanDir($ARGV[0]);

print "Total size is " . $totalSize . " Bytes\n";
print "Last file modified was: " . $lastModName . "\n";
print "Last mod was " . $lastModTime . "\n";
print scalar localtime($lastModTime); # easy conversion, but does not list timezone EDT
Undefeated TL Tecmo Super Bowl League Champion
supereddie
Profile Joined March 2011
Netherlands151 Posts
Last Edited: 2012-08-12 17:00:19
August 12 2012 16:59 GMT
#3106
Personall I would count the totalSize within the scanDir function and return it as a value. Something like:

sub scanDir {
....
my $totalSize = 0;
....
for .....
$totalSize += scanDir if isDir;
$totalSize += evaluateFile if isFile;
....
print "total size (incl. subfolders) of dir " . $dirName . " = " . $totalSize . " bytes";
return $totalSize;
}

....
print "Total size is " . scanDir($ARGV[0]) . " Bytes\n";

I feel like it would be easier to maintain/expand later on if needed.
"Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design
EscPlan9
Profile Blog Joined December 2006
United States2777 Posts
August 12 2012 17:08 GMT
#3107
My professor is really specific with the formatting and instructions. He would mark off if I did something like that, though I understand what you're getting at. My output exactly matches his required now and I've submitted the script in the dropbox. Thanks for all the help!
Undefeated TL Tecmo Super Bowl League Champion
SiPa
Profile Joined July 2010
Germany129 Posts
August 13 2012 11:45 GMT
#3108
Quick Question:
I have a few lines of code, written in C++.
I have to write code, that does the same stuff in C.
the C++-code contains vectors (std::vector<>)
It uses the push_back-function and clear-function of the vector.
Is there an equivalent to the C++-vector in C? (Array with variable size?)
Google pretty much tells me to make my own thing.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
August 13 2012 11:53 GMT
#3109
On August 13 2012 20:45 SiPa wrote:
Quick Question:
I have a few lines of code, written in C++.
I have to write code, that does the same stuff in C.
the C++-code contains vectors (std::vector<>)
It uses the push_back-function and clear-function of the vector.
Is there an equivalent to the C++-vector in C? (Array with variable size?)
Google pretty much tells me to make my own thing.


There is no equivalent. You can make your own implementation of a variable size array, though depending on the actual use, it might actually be easier to go with a linked list instead.
heishe
Profile Blog Joined June 2009
Germany2284 Posts
Last Edited: 2012-08-13 12:05:12
August 13 2012 11:59 GMT
#3110
On August 13 2012 20:45 SiPa wrote:
Quick Question:
I have a few lines of code, written in C++.
I have to write code, that does the same stuff in C.
the C++-code contains vectors (std::vector<>)
It uses the push_back-function and clear-function of the vector.
Is there an equivalent to the C++-vector in C? (Array with variable size?)
Google pretty much tells me to make my own thing.


There's not, but things without inheritance are convertible to C in a pretty straightforward manner (and inheritance is easy, too, just a bit bothersome), with some handicaps. But templates just won't work, you flat out won't be able to get that in C. So if you want multiple types to be handled by that vector, you either have to implement it multiple times each time for one certain type, or you have to store void* in the pointer and cast ("with certainty") around to get the types into and out of that you want. The rest works like this:

If you have some class with some method, e.g.


class vector
{
attribute a;
attribute b;
attribute c;
public:
//...
size_t size()
{
//...
}
}


it can be done by creating a struct vector and a free method size(vector* vec);


struct vector
{
attribute a;
attribute b;
attribute c;
}

size_t size(vector *vec)
{
//...
}


Similarly, methods with parameters would transform from return_type class::method(a,b,c) to return_type method(class*,a,b,c) (this is actually exactly like it looks in assembly code, without optimizations). Constructors can be implemented by simply making a free method Constructor[yourstructname](struct *str) or something.

Of course, usage changes too, from classobject.method(params); to method(&classobject,params);

Converting the vector class like this should be trivial, since you can look up its source code of whatever compiler/IDE you're using.

However, you can not recreate other built-in features to C++ like RAII, unfortunately.
If you value your soul, never look into the eye of a horse. Your soul will forever be lost in the void of the horse.
waxypants
Profile Blog Joined September 2009
United States479 Posts
Last Edited: 2012-08-14 02:11:39
August 14 2012 02:05 GMT
#3111
On August 13 2012 20:45 SiPa wrote:
Quick Question:
I have a few lines of code, written in C++.
I have to write code, that does the same stuff in C.
the C++-code contains vectors (std::vector<>)
It uses the push_back-function and clear-function of the vector.
Is there an equivalent to the C++-vector in C? (Array with variable size?)
Google pretty much tells me to make my own thing.


Not in standard C. I like to use glib for generic data structures among other things.

http://developer.gnome.org/glib/stable/glib-data-types.html
http://developer.gnome.org/glib/stable/
Olsson
Profile Joined April 2011
Sweden931 Posts
August 15 2012 12:41 GMT
#3112
Does anyone know a good website to learn C#? You guys got me interested, I tried C++ abit but I think it was too much like most of you guys told me.
Naniwa <3
bo1b
Profile Blog Joined August 2012
Australia12814 Posts
August 15 2012 19:02 GMT
#3113
I just noticed a new edition of c++ primer was released, does anyone think it would be a good idea to buy it or are they're better resources for learning c++?
IreScath
Profile Joined May 2009
Canada521 Posts
Last Edited: 2012-08-15 20:53:55
August 15 2012 20:38 GMT
#3114
edit: nvm... I figured it out.
IreScath
tofucake
Profile Blog Joined October 2009
Hyrule19264 Posts
August 15 2012 20:40 GMT
#3115
I'm sure this doesn't really help you at all but personally I hate handbrake.

...yeah
Liquipediaasante sana squash banana
IreScath
Profile Joined May 2009
Canada521 Posts
August 15 2012 20:53 GMT
#3116
On August 16 2012 05:40 tofucake wrote:
I'm sure this doesn't really help you at all but personally I hate handbrake.

...yeah


open scource + opencl = yay.

but I think I go it now.
IreScath
SiPa
Profile Joined July 2010
Germany129 Posts
Last Edited: 2012-08-16 09:11:28
August 16 2012 09:02 GMT
#3117
GLib is awesome!
Now some questions regarding GLib:
I get tons of "expression must be an lvalue or a function designator" and "a nonstatic member reference must be relative to a specific object" errors. I get, that this isnt necessarily connected to GLib, so i will just give you the code:
	struct trajectory{
GArray *position1;
GArray *position2;
GArray *velocity1;
GArray *velocity2;
position1 = g_array_new(FALSE, FALSE, sizeof (double));
velocity1 = g_array_new(FALSE, FALSE, sizeof (double));
position2 = g_array_new(FALSE, FALSE, sizeof (GArray));
velocity2 = g_array_new(FALSE, FALSE, sizeof (GArray));
_Fragment fragment;
};

	
GArray *startVelocity;
startVelocity = g_array_new(FALSE, FALSE, sizeof(double));
g_array_append_val(startVelocity, g_array_index(iDir, double, 0) * iFragment.Speed);
g_array_append_val(startVelocity, g_array_index(iDir, double, 1) * iFragment.Speed);
g_array_append_val(startVelocity, g_array_index(iDir, double, 2) * iFragment.Speed);

g_array_free(trajectory.velocity2, TRUE);
g_array_append_val(trajectory.velocity2, startVelocity);
g_array_free(trajectory.position2, TRUE);
g_array_append_val(trajectory.position2, iPos);


iPos and iDir are pointers to GArrays.
 trajectory calcTrajectory( _Fragment iFragment, GArray *iPos, GArray *iDir ){}


Errors are at g_array_append_val, velocity2, position2

I hope some1 could provide help, thx in advance!

Edit: I also would love to know how to get the size of a GArray, meaning how many Elements it contains.
waxypants
Profile Blog Joined September 2009
United States479 Posts
August 17 2012 03:18 GMT
#3118
Don't have time to figure out what you are trying to do right now, but I can tell you:

1)
position1 = g_array_new(FALSE, FALSE, sizeof (double));

In C, you can't have member initialization inside the struct definition.


2)
trajectory.velocity2

In C, you can't have static struct members.
waxypants
Profile Blog Joined September 2009
United States479 Posts
Last Edited: 2012-08-17 03:52:30
August 17 2012 03:50 GMT
#3119
This is a response to a PM about where to get Glib. I put installation instructions for Visual Studio. These instructions are actually pretty much the same for most of the "open source" projects you may want to download and use with Visual Studio. Let me know if it doesn't work and I will try to fix my instructions if I left something out or something.


You can build it yourself (enormous pain), or get it from here:

http://www.gtk.org/download/win32.php

Not the newest version, but fairly recent. You may be able to find a newer version elsewhere but this one has worked well enough for me.

I'm assuming you are using Visual Studio. In that case you will need to do the following:

To build your program:
1) Download the Glib "Dev".
2) Extract it to something like C:\glib-dev_2.28.8-1_win32 (or whatever directory you want)
3) In your project properties, go to Configuration Properties->C/C++->General and add "C:\glib-dev_2.28.8-1_win32\lib\glib-2.0\include" and "C:\glib-dev_2.28.8-1_win32\include\glib-2.0" to the list of Additional Include Directories.
4) In your project properties, go to Configuration Properties->Linker->General and add "C:\glib-dev_2.28.8-1_win32\lib" to your list of Additional Library Directories.
5) In your project properties, go to Configuration Properties->Linker->Input and add "glib-2.0.lib" to your list of Additional Dependencies.

For steps 3 and 5 you may need to add other directories and/or other lib files to the list, but these things have been enough for what I have done. You will know what you need to add whenever it complains that it can't find "blahblah.h" or "blahblah.lib".

To run your program:
1) Download the Glib "Run-time"
2) Extract whatever files you need from the bin directory into the same directory as your program's exe file. You can add one at a time until your program is able to run, or you can just copy all of them if you don't care about having extra unnecessary files. In theory, I think could also put them in C:\Windows\System32 if you want, and then you won't have to copy them to all of your projects, but if somebody else wants to run your program they will need to download the necessary dll's.
waxypants
Profile Blog Joined September 2009
United States479 Posts
August 17 2012 11:23 GMT
#3120
The lvalue error is because the second argument in g_array_append_val has to have an address (in other words it has to be a variable). It's because it seems that they have defined g_array_append_val as:

#define g_array_append_val(a,v)	  g_array_append_vals (a, &(v), 1)


The &(v) is what is throwing the error. Anyway it sucks, but you can do something like:

double speed;

speed = g_array_index(iDir, double, 0) * iFragment.Speed;
g_array_append_val(startVelocity, speed);
speed = g_array_index(iDir, double, 1) * iFragment.Speed;
g_array_append_val(startVelocity, speed);
speed = g_array_index(iDir, double, 2) * iFragment.Speed;
g_array_append_val(startVelocity, speed);


Or even:

double speeds[3];

speeds[0] = g_array_index(iDir, double, 0) * iFragment.Speed;
speeds[1] = g_array_index(iDir, double, 1) * iFragment.Speed;
speeds[2] = g_array_index(iDir, double, 2) * iFragment.Speed;
g_array_append_vals(startVelocity, speeds, 3);
Prev 1 154 155 156 157 158 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 5h 12m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
WinterStarcraft511
RuFF_SC2 172
StarCraft: Brood War
Rain 2872
GuemChi 1453
NotJumperer 69
Mind 49
Bale 29
Aegong 22
Terrorterran 6
Icarus 5
Dota 2
monkeys_forever428
Other Games
summit1g6291
C9.Mang0401
Organizations
Other Games
gamesdonequick735
[ Show 12 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Migwel
StarCraft: Brood War
• Pr0nogo 10
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• lizZardDota2105
League of Legends
• Stunt436
Other Games
• Shiphtur583
Upcoming Events
The PondCast
5h 12m
Kung Fu Cup
6h 12m
IntoTheTV X SOOP
1d 6h
Reynor vs Zoun
Neeb vs Rogue
FEL
3 days
Patches Events
3 days
AI Arena Tournament
3 days
FEL
4 days
Sparkling Tuna Cup
4 days
Replay Cast
4 days
WardiTV Weekly
5 days
[ Show More ]
IntoTheTV X SOOP
6 days
ByuN vs Lambo
MaxPax vs Bunny
PiGosaur Cup
6 days
Liquipedia Results

Completed

ASL Season 22
Blizzard Classic Cup 2026
HCC Season 3

Ongoing

Acropolis #5
IPSL Winter 2026-27
KCM Race Survival 2026 Season 4
Acropolis #5 - GSC
1win Private Club #2
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Play Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026

Upcoming

BSL Season 23
SC4ALL II: Brood War
BSL 23: Non-Korean Championship
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
eXTREMESLAND 2026
PGL Major Singapore 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
IEM Beijing 2026
Stake Ranked Episode 5
PGL Masters Bucharest 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.