There was some comparison with Python yesterday. Though perl is not as good as python in many ways for some reason perl has a very strong developer community around it. It has got a certain way to obtain copious documentation in UNIX man page format.
Perl has CPAN(www.cpan.org) which is integrated into the standard distribution. The sheer energy and force behind perl seems enormous. No other language has enjoyed such sheer patronage. Not C, not java,not even php. And perl's UNIX focus lies in its manpage format for documentation unlike python's help(<module>) style, though every language has OS bindings perl is a direct copy when it comes to regex, its IPC and so on. It is nothing but UNIX in a new flavor. Today we will see some more examples. $ cat 0.pl ------------------------------------------------------- #!/usr/bin/perl $scalar1 = "This is a string scalar"; @arr = (1,2,3,5, "string1", "one more", $scalar1); %h = ("key1" => 6, "key2" => 10); print "The array is @arr\n"; for(keys %h) { print "key is $_, value is $h{$_}\n"; } ------------------------------------------------------- I will explain the above code. We have seen scalars yesterday. All scalars start with $ and hold singular values. If you want to store a list of values you require arrays that start with the @ character. Then we see the hash which starts with the % and it uses the same assignment style. Meaning that we use (...) to assign values to the hash. This is because hash is also an array. Instead of numeric prefix it uses named keys. That is why it is called an associative array. So it has exactly 1/2 of the array length. This is because half of the assignment items go towards making the key and half the values. Hashes have no ordering. It is stored and retrieved in a different order. So in order to use ordering we need to resort to the sort() function in perl. We saw that the array can be printed just by enclosing the array in double quotes("). If you print it like this: print @arr; you will get the same result without the space between array elements. This is a key feature of perl. Perl is context aware. Depending on whether the lvalue is singular or plural(scalar or array), the RHS will be assigned as the length of the RHS or the list in the RHS itself. For instance, if you write, $sca = (1,3,4,5); You will see that $sca contains 4. $ perl -e '$sca = (1,2,3,4); print $sca;' 4$ But if you do this: @a = (1,2,3,4); you will be able to assign the whole array. This can trip many people, even experienced programmers. One can even argue it is a bug. For instance in many situations it will be obvious we want the entire list but you won't get it without resorting to () on the LHS. Anyway we will get to that on a future date. For now, the hash statement and the print in the above code is confusing. %hashsample = (1, 100, 2, 200); will get assigned as : 1 -> 100 2 -> 200 So, $hashsample{1} = 100; $hashsample{2} = 200; Note the syntax. This will drive you mad initially. Perl uses a lot of funny characters. One more thing. Another confusing thing but you can say it is a feature. You assign the hash like this also and get the same result. %foo = (1 => 100, 2 => 200); This is because => is an alias for ,. Howzzat? Anyway array values are accessed with [<index>] and hash values are accessed with {$key}. For example, @a = (1,3, 10); print "first value is $a[0]\n"; %foo = (1 => 100, 2 => 200); print "hash content with key 1 is $foo{1}\n"; Since hash always contains scalars as keys and could contain anything as value it is always one to one. For instance you can hold an array or function pointer as value but it has to be an array pointer. So every time you access a hash value, it will be $hashname{$key}; The usage, for(keys %h) { print "key is $_, value is $h{$_}\n"; } will be explained tomorrow in iterators. This is the foreach() iterator. Perl makes it very convenient to use array elements in a for loop. More tomorrow. -Girish -- G3 Tech Networking appliance company web: http://g3tech.in mail: gir...@g3tech.in _______________________________________________ ILUGC Mailing List: http://www.ae.iitm.ac.in/mailman/listinfo/ilugc