‏إظهار الرسائل ذات التسميات Programming. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات Programming. إظهار كافة الرسائل

السبت، فبراير ٢١، ٢٠٠٩

Perl, You don't want to miss! -3-

Lists and arrays in Perl.


That will be my last post about Perl, however Perl doesn't stop here :) ... you may notice I didn't mention the strength of Perl string processing and file handling. It's worth searching for ;)

What about list?
I may have mentioned before that lists could be generated easily this way in Perl:


(1..5)



This is a range list, it's also valid for character types



What about:

(c..10)



This one is valid too! since "c" is interpolated to Zero (0), then it becomes: (0..10)



Lists in general could be written this way:

('Item1', 'Item2', 'Item3')

or

qw(Item1 Item2 Item3) #you may use any delimeters for this method rather than the parens



Indexing the list is much like any other language you know,using square brackets. The two things that might be of interest in Perl:

- Using a floating number to index, will be floored

- Using a negative number will index starting from the end of the list (backward)

- You may user a list of indexes! this will return another list for the indexed positions (list slice)

- You may combine slicing and ranging to construct the list of indexes. [1,2,3 .. 7]



Assignable lists:

($firstItem , $secondItem) = (1,2);

print $firstItem; #This should print '1'



Array type:

@arrayName = (1,2,3,4);

print @array,"\n"; #Output: 1234

print "@array\n"; #Output: 1 2 3 4 #Notice the spaces! Amazing



Indexing an array variabe:

use "$" to tell Perl what you "want" to get out of the variable.

$firstItem = $arrayName[0]; #Notice the usage of the '$' when indexing the array

#That's because it's returning a single/scalar value;



The "scalar" function may take care of this too.



What's the array's last index? $#

So for example, for array "@arr", that last element's index is: $#arr



List methods:

- shift: Add element to the front

- unshift: Get & remove element from the front

- push: Add element to the back

- pop: Get & remove element from the back

- sort: returns a sorted array, be default it sorts using the string comparison methods.



sort { <:method expression>} @arrayName



So it would be for example:

sort { -($a <=> $b) } @array;

This will return a sorted array, in descending order.

Perl: You don't want to miss! -2-

This post will be about loops and conditions! Hopefully this will be too short.

Loops and if conditions are not much differnt from other langauges:
while, for, do-while, if, foreach

What seems new to me (Cx & Java developer)
- if -elseif
- until (negative of "while": while not)
- do ... until
- unless (negative of "if": if not)

What about loop controls?
- last (aka "break")
- next (aka "continue")
- redo (the amazing keyword, just redo the loop!)

while(<STDIN>) #Keep reading the standard input
{
print; #No argument? yes, it defaults to printing the magic variable: $_
if($_ > 0) {
$_--; #Decerement
redo; #Back to the top of the loop without executing the condition body!
}
}

Expression Modifier
What about rephrasing the "redo"?

redo if $_-- > 0; #Remember, the post-decrement has the lower precedence
See, the statement (action) comes before the condition in this expression modifier.

This is valid for the "if" and "unless" too. Give it a try!

Perl: You don't want to miss! -1-

Assalamo Alikom,

This post is about Perl, a well known programming language, and you don't want to ask me about it! I'm still a beginner doing the book execrices, didn't finish my assignments yet.
Better you go google "Perl": A {Scripting Dynamic} Langauge

This post is intended mainly to mention the first facts I've learned about Perl, it'll be all about the "print" function and some Strings and Numbers!

Print:
Here's the first picture of "print":
print "Hello... you know!";
This will print: Hello... you know!

Trivial, hah? even this:
print 'Hello... World!'
This will print: Hello...World!

What if we embeded this (scalar) string $myName inside the first version of print:
$myName = "Muhammad";
print "Hello $myName"

The second version?
$myName = "Muhammad";
print "Hello $myName"

This didn't automatically convert my string, good!

What about this:
print `Hello World!`;

These are different quote styles: `
This should output something like: Command not found ... Yeah, this print will execute what's inside!
So you may try something like:
print `cp file1 file2`;

("cp" is the copy command for bash)

What about Strings/Numbers?

Strings in Perl are First-Class Citizens! users shouldn't treat them as a character array!

$stringOne = "1";
print $stringOne + 1;

This prints "2" ;)

What about this:
$stringAddress = "8A";
print $stringAddress + 8;

This will print a warning! and then? ... it will print "16" which is 8+8
Perl starts parsing the string until it finds a non-numeric value it terminates but it keeps the valid piece. Notice that if the address string was something like: "A8" it would be converted to "0"!

Incrementing strings:

print ++"A0";

This will output: A1
You should give it a try yourself ;)

Hopefully I'll have more to say later on :)

Assalamo Alikom

الجمعة، مايو ٢٦، ٢٠٠٦

Archigos revisited

el salamo 3alikom,

I've talked before about new things we have learned/used,
now i'm gonna talk about the purpose of the project itself, and before i'll try to explain what's the 'Computer Architecture' about,,,
Amigos team and Wael Abu El-Kheir (Compter & Info. Sciences-Ain Shams, 3rd Year students) worked in that project, trying to make something useful for architects and students or doctors to check their designs ;)... correct design -> correct simulator results, and vice-versa :D

What's the computer architecture?
A computer contains a CPU that process all data, Memory that stores data, and other devices such as hard-disk, I/O devices...
First, the architecture deals with how that processor works, or how to make it work... how to make it understand that a given string of 0's and 1's means ADD or SUBtract or whatever?
Second, it start looking for how to manage the processor activities with other devices arrond

A written code (Assembly, C, etc...) is first Assembled/Compiled , that produces the '.obj' file containing 0's and 1's, each code instruction has it's 'opcode' (string of 0'sand 1's) that identifies it, those binaries are stored in memory then read by the processor as input to the CLU (Control Logic Unit), that unit takes input, then inside it performs a number of logical operations, and outputs to the control lines (increment, clear, load, read, write) of the desired Register, RAM, MUX, etc..
That's all about architecture.

What Archigos do ?
The idea is to make a dynamic emulator, we may call it simulator. That takes the computer architecture design description as input, then it simulates that desgin. i doubt u got what i mean :D !
ok, the design contains some wires that are inputs to some logical gates and are not output of any logical gate,,, those wires are 'pure' inputs, that means i should feed it with there values for the CLU to work
other wires are output of some logical gates, and are not input to any other gate, those are called pure output, outputs on those wires is what i need to control my computer components
I give the simulator the CLU description, give it input, connect output wires with control lines of componenets i represent as objects in my simulator, then press 'Run' it shows the effect of given input on the computer components.
I wish it's somehow clear now :D

we used Electronics Workbench to draw the CLU and to transfer it to Netlist file, we parse (.net Regex) that netlist to produce a C# code (.net CodeDOM library) , all is logical expressions
Compiling then Invoking the generated function (.net Reflection) , returns output that is connected to my objects control lines (object references -pointers do undefined behavior :( )
That's it all.

The compuer components are till now statically repreented inside the code, that's the computer components are code defined, and so connections between components
We look to make that dynamic, no more components are defined inside the code, a user then should create the component he would like to add to his computer, and its control lines will appear for matching with the design wires,,, and so entire connections between components gonna be done graphically

Some screen shots: ( you won't notice any funky interfaces :D )


I didn't talk about ALU as i actually forgot it :D, it's that part of the computer that performs arithmetic and logical operations, it has its own parsing techniques cuz we won't draw it all, we just draw its first, and last stage , and the ith stage, that's gonna be repeated n-times

other files are the CLU design description
pressing next parses these files, and determines if there is 'resursive' connections or 'duplicate' output names (those conditions are denied).

left grid is for pure input wires and right one is for outputs (an option above is available for showing non-pure outputs too)
left column of each contains wires determined in the design, right column are control lines of objects in application
I may save matching for later use, and also names that nearly look like each other are matched automatically
Pressing next, generates the C# code, and sort statements (as some output maybe input to another statement, it should be evaluated first)
any errors (all input wires should be matched, warning that maybe some control lines are not connected to any wire), the architect is notified
Then running the simulator
The simulator performs on the latest loaded design, the architect may move step-by-step are run the appication till a breakpoint or halted

A downloadable (source and executable) version will be available soon isA

That's it :D
And now i should run to start studying for final exams :

salaaaaam :D


الخميس، مايو ٠٤، ٢٠٠٦

C# Pointers

el salamo 3alikom wa ra7mato Allah wa baraktoh,

how are you everybody? fine e7l

Pointers is one of many other programming language capabilities that every big system developer will need,
Inheritance, Virtual functions, and alot of keywords that when we read about years ago, we said
"structure! , that's making working more complex, i should put some extra text and a dot!"
"making a function private!!, i should not make it at all then!"
"pointers!, why should a have the variable address, i'm dealing with it's value!"

ok, years passed, and someday will come to recognize how important those things are.
Now, i'm gonna talk about how important are pointers and how are they in C#

Pointers:
Assuming a have an electronic connection, node "A" is connected to node "B", and node "C" is connected to node "A" too.

the value of "A" is calculated somehow,,, when representing these nodes as ByValue variables in ur code, then each time u gotta Update values of B and C to have the new value of A!
Update(ref bool B, ref bool C)
{
B = A;
C = A;
}
Good, but what if I've initialized B and C with the address of A ?
bool* B;
bool* C;
B = &A;
C = &A;

any changes to A gonna be reflected to automatically and seen through B and C
Pointers are not only used for dynamic memory allocation ( i think that has not become a matter in C#, as its default is dynamic by reference allocation for the majority of classes).

What do u think the action taken by the C# compiler when you write code as above ? ... pointers in Unsafe scope!!... you should specifiy that part of code to be unsafe, rick region :D
you could specify the scope of function, class, etc... by preceding it's declarator with the keyword 'unsafe'

unsafe public void func(...)
{
//use pointers asif u r in C++, only one restriction! which is...........
}
u have to initialize the pointer with address of some variable (no just 'new' address allocated for the variable)
//Error: int* ptr = new IntPtr(0);

//Correct:
int init = 0;
int* ptr = &init;
//then work with 'ptr' freely from here on, inside ur unsafe scope

u could make objects of classes contining that variables in scopes that shouldn't be unsafe (it's enough for the declaration scope only to be unsafe)

Does the garbage collector affect our pointer ?
Yes, as the variable you have assigned it's address to that pointer is of no more use, then the pointer is aloso of no more use :D
that's a good point, u don't have to 'delete' the pointer,

u may make it 'fixed' ... how ?
fixed (int* ptr = &init)
{
//ur dealing with 'ptr' here
}
i've not used 'fixed' and don't know alot of details about it, u can simply ask Dr.Google about it :D

after you have written ur unsafe code, u have to enable unsafe code compilation
Project > [project name] Properties...> 'Configuration properties' --> 'Allow unsafe code blocks'=True

that's it for now :D , wish it's useful :)

el salamo 3alikom wa ra7mato Allah