On Fri, Dec 9, 2011 at 8:24 AM, Girish Venkatachalam < girishvenkatacha...@gmail.com> wrote:
> I have not yet understood how map() works. It is a functional > programming concept. > map is a means of transforming ("mapping") an array into another array. The arguments to map are: 1. function which will take one element at a time and return one element 2. the array of elements As an example, if one would like to calculate the square of numbers in an array and store them in another array, one can use map to do it as follows: @x = (1,2,3,4,5); @y = map { $_ * $_; } @x; or simply @y = map { $_ * $_; } (1, 2, 3, 4, 5); When map invokes the provided function (first arg) it is called with $_ set to each element of the array and is called one time for each element in the order of appearance of its arguments. Let's use map now to also print the array @y: map { print $_ . "\n"; } @y 1 4 9 16 25 Here is an exercise to the reader: Using a combination of regexes and map, read a HTML file and turn all its <a href="(.*?)">(.*?)</a> into a hash where the URI is the key of the hash and the marked up text (anchor text) becomes the value. The following is a sample test input string: $string = <<EOM; <a href="/blah">Go to blah</a> and <a href="/blah/search">look for all the blahs</a>... and here is a <a href="/multi">multi line <em>nested</em> HTML markup </a> EOM In a lot of ways perl is messy and with a syntax that is so odd that > it is often called > > "The write once forget language" > > In other words perl is used for quick and dirty jobs. > I'd once again request you to stop demeaning the stuff you're trying to teach (or thrust your impartial views onto a public forum like this). Why would anyone want to learn something that is, according to your narrow understanding, is worthless? If you think something is tough, it will remain tough and unapproachable (because the problem of 'toughness', etc., is in your mind). If you're full of your own preconceived notions, it is very difficult to change it unless you're willing to throw away those notions and then explore and learn about it (there is nothing to lose when learning, except time :) ). Think about it, programming a computer was not any friendlier back then, yet, that is what paved way for generic abstractions that allowed us to build cooler stuff on top. Perl too, if you approached it this way, can be effectively used to abstract away problems and build generic solutions on top. But you should be willing to invest time in abstracting and designing your code thus. If you expect a scissor to do what a chainsaw can (and vice versa) then it is not the tool that is at fault. Perl on the other hand is a high level language. It is a scripting > language; which means that it is a > language used to express your ideas in a language that looks like English. > > This could be said of python but perl is not even half as elegant. > An illiterate will (almost always) write horrible poetry. This is a highly subjective (and highly inflammable ;) ) bait. Perhaps you're judging perl by your (or your peers') inability to write elegant code? A sword, to a Samurai, is more elegant than a pistol. > Why was I not forced to learn Python deeply? But perl is something I > use everywhere. This should give you a hint. > For that matter, why were you not forced to learn even perl deeply? Cheers, -Suraj -- Career Gear - Industry Driven Talent Factory http://careergear.in/ _______________________________________________ ILUGC Mailing List: http://www.ae.iitm.ac.in/mailman/listinfo/ilugc