Archive

Posts Tagged ‘for’

Multi-Argument Parsing

March 15th, 2010 admin No comments

Well, it has been some time since my last post since I have been (and still am) very busy, but I felt I needed to update! I wrote a quick script for you guys to help in any programming endeavors you may be having parsing your command line arguments in any command line program. This example merely shows how to simply print all the arguments given to a specified program, however, you can use this type of loop to do whatever you wish!

/**
* Process Command Line Args
* Level: Easy
* Author: Dennis J. McWherter, Jr.
*
*/

#include <iostream>

using namespace std; // I'll save myself some std::<> pain this time XD

void echo(char* str,int no)
{
int x = strlen(str);
cout<< "Argument Number "<< no << ": ";
for(int i=0;i<x;i++){
cout<< str[i];
}
cout<< endl;
}

int main(int argc,char* argv[])
{
for(int i=1;i<argc;i++){
echo(argv[i],i);
}
cout<< endl<< "Command line: "<< argv[0] << endl;
return 0;
}

As you can see, the first function is a loop which is given a char* array (it loops through each array value to print out the full string) and an integer position. The int allows the script to know which argument number it is processing (not that it is imperative in this case). In the int main(); function, this void echo(); function is called in another for(); loop. This loop begins at array position 1 (the array starts at position 0, however, arguments begin at 1; 0 is the program/command line name). Therefore, for each argument listed the program loops until it reaches it has no further arguments to parse.

There is your quick lesson today in processing multiple arguments in the command line! This should help many of you looking for a quick and easy solution to do this! The example code is provided below for download.

Regards,
Dennis M.

Parse Multiple Arguments

Understanding Arrays and How to Use Them

June 24th, 2009 admin 1 comment

So in a recent project I have just finished up (again, will let you guys know what it is when it’s officialy released), arrays had become my best friend along with some regex, but that’s a topic for another post. I needed to turn strings into arrays, which I would then put back into arrays, and finally back into strings. I did this in a few different ways, but let’s go into some examples of why one would need to do this, then we’ll begin the how; seeing as the how without the why makes very little sense.

Well, I mainly use all this to transfer large amounts of separated data from function to function. In my most recent endeavor it was to split a specific type of file, parse it through 3 different functions (each function returned an array, but dealt with the data in strings which I will explain) then finally took all the information in the provided array and successfully dumped the data to a database. This is only one of the numerous reasons a person could/would use an array, but they help like one couldn’t imagine.

Let’s begin with the basics. For sake of example, we’ll keep things in PHP. The first example below just explains an array. No more, no less.

<?php
// Ways to add data to arrays!
$array1 = array("Entry 0","Entry 1","Entry 2","Entry 3"); // Just put data in
$array2 = array(
0 => "Entry 0",
2 => "Entry 2",
1 => "Entry 1"
); // Define arrays by opening and stating index.
$array3[0] = "Entry 0";
$array3[1] = "Entry 1"; // Enter directly into index

?>

Another way to build an array, more or less indirectly, is to use php’s “explode();” function. This will take text with a defined delimiter and separate each set of data (at the delimiter) into its own index. For example, consider the following.

<?php
$string = "This is a string where we will, use commas, for delimiting the text and to, split this into an, array!";
$array = explode(",",$string);
print "Original: ".$string."<br /><pre>Array: ";
print_r($array);
print "</pre>";
?>

To call these defined arrays we would use $array[INDEX]. Now, there are many ways to access this data and filter it. Let’s begin by calling one value from an array.

<?php
$array = ("Entry 0","Use this text","Entry 2","Entry 3","This text isn't being called either");
print $array[1];
?>

The script above with print “Use this text” because it is in the array at the index value 1. It is important to remember that indexes start at 0 unless defined otherwise. Now, we’ll explain some more intricate ways of getting all your values. The first way is iteration using the “for();” loop. Here goes:

<?php
$array = ("Test","Test2","Word3","Keep 'em coming!","white space next :P ","","No need for that","We <3 trim");

// Iterate.
for($i=0;$i<count($array);$i++){
print $i.") ".$array[$i]."<br />";
}
?>

Now this will just result similarly to the foreach(); which I am about to explain. However, sometimes iteration is more effective than foreach(); because it gives you more liberty. Most of the time I would recommend iteration for multi-dimensional arrays (arrays within arrays and so on), but usually two different ones. For example:

<?php
$array = array(array("Array in index","number 0"),array("Array in index","number 2"));

for($i=0;$i<count($array);$i++){
// Now, let's get our multidimensional values! :D
for($ii=0;$ii<count($array);$ii++){
print "[".$i."][".$ii."]: ".$array[$i][$ii]."<br />";
}
}
?>

Now, that will grab all the data within the arrays within the arrays. Wow, sounds confusing, but once you get a better grasp, it’s really quite simple, but I cannot lie; it all seems very daunting at first. Now we’ll explain the foreach(); function. This function basically does what we just did manually, but sometimes is easier and more effective. A lot of times, you will use the two in conjunction (even if for different purposes), so become friendly with both of them!

<?php
$array = array(
0 => "Entry 0",
1 => "Another entry",
2 => "Again...",
3 => "And finally :P "
);

foreach($array as $key => $value){
print "Key/Index: ".$key.": ".$value."<br />";
}
?>

From the code above, you can see that the foreach(); function has accomplished the same feat we did earlier with iteration. On a simple array such as this, it would be best to use foreach, but like I said: it may be necessary at times to use iteration.

As I mentioned earlier, both can be used in conjunction. Just a quick example (we won’t go through the actual coding of it) is if you have a multi-dimensional array where you feel the need to use foreach, you can use foreach($var[$i] as $key => $val){} and work it that way.

So all in all, everything boils down to: love iteration, love foreach(); on the programming languages it’s available, and above all LOVE arrays! They are right next to regex in the developer’s “most useful tools” section of his or her tool belt!

Regards,
Dennis M.

Categories: Other, PHP Tags: , , , ,

Busy With Working… Taking More!

June 19th, 2009 admin 2 comments

Hey everyone,

As you can see, it has been a long time no post XD. It’s simply because I’ve been very busy with work and projects you guys have been giving me. Within the next week I should be done again and ready for you guys to continue contacting me for more projects. Also, in between projects I will be posting some more blog posts to help everyone. With every project I get I gain greater ideas on what you guys would like to learn.

As for next time, I have a big post planned. I’ve been working on a fairly large project for almost 2 weeks and am finally finishing up. I won’t say much about it until it’s released (and then I will make a whole post about it :) ), but it’s a great script and definitely will be worth your while to take a look at.

Thanks for checking!

Regards,
Dennis M.

Categories: News Tags: , , , , , ,

The For(); Loop

May 13th, 2009 admin No comments

Many new programmers are perplexed by the “for” loop. However, it is one of the most essential and powerful tools in any developer’s arsenal. Without it, efficient and clean programming would not be as possible. Early on, most developers seem to stick to the while(); loop. I mean sure, while(); is great for some things, but may not exist in some programming languages and not to mention won’t work for everything you need.

Let’s begin by going into the basics about the for(); loop. Well really, a lot depends on the programming language (I will provide both PHP and C++ examples in the source file). But more or less, the for(); loop is simply iteration. It will run until its defined limit is hit providing whatever results you wish. We’re going to do this in more standard languages (e.g.: C/C++ and Java) instead of PHP for mass compatibility. Just note, if you’re looking to do this for php, you do not need to define the type of variable, and your variable will just start with the “$” symbol. So for example, PHP would be for($i=0;…;…){}.

Now we’re going to write a sample program and break it down:

for(int i=0;i<=5;i++){
std::cout<< "This is line number "<< i << std::endl;
}

Now as most of you may have noticed, this is a C++ example, but will effectively explain how the loop works for ALL programming languages!

int i=0; – This simply defines the variable and starting place (0 in this case). In PHP you would simply use $i=0; and so on for the rest of the statements.
i<=5 – This tells the loop when to stop (This <= symbol means less than or equal to). When i>5, the loop will stop and proceed on to the rest of the code.
i++ – This tells what the loop should do after every time it is run. In this case, we’re using i++ which means to add 1 each time around.

Now those are the basics to the loop. What lies within is just simply the code to be executed, which could of course have conditionals and such within it as well (e.g.: if(), etc.). So now we’ll move on to a more complex example.

unsigned int choices=5;
char* choice = new char[choices];
for(int i=0;i<=choices;i++){
std::cout<<"Please enter a value: ";
std::cin>>choice[i];

if(!choice[i]){
std::cout<<"No value for choice "<< i<< std::endl;
}
}
for(int i=0;i<=choices;i++){
std::cout<<"Choice "<< i<<" was "<< choice[i] << std::endl;
}

This will store an array of choices and then after entering them, they will be printed back!

In the source below, all working examples will be provided!
For(); Loop Tutorial

So, that basically wraps everything up! If you need anymore clarification, just let me know.

Regards,
Dennis M.

Categories: C/C++, Java, Other, PHP Tags: , , , , , ,