in reply to Re^3: reading data ...
in thread reading data ...
Which means, "read each line of input from <DATA>, and for each line, pass it through the map block, and what comes out the other end, assign that to @data." The map block defines what happens to each line of input. The final value evaluated in the block -- its result -- is what "comes out the other end". So here, we chomp the line, split it on commas, and stuff that resulting list into a new, anonymous array. The result of the block (for each iteration, mind you) is the reference to that array. In short: for each line in, get an array-ref out. The enhancement is almost identical; we're just doing a bit more inside the map block:my @data = map { chomp; [ split ',' ] } <DATA>; Can reformatted as my @data = map { chomp; [ split ',' ] } <DATA>;
Instead of just schlurping the result of the split directly into the new anonymous array, we're passing those list items through a map. The point of this map is to strip off leading/trailing quote pair, if it exists. That's what the regex is for. If the match succeeds, "keep" the part inside the quotes; if it fails, keep the original string. (By "keep", I mean "Let the map block result in". Hope that's not too confusing.) It seems like obfuscation, but it's really just plain, albeit dense, idiomatic perl.my @data = map { chomp; [ map { /"(.*)"/ ? $1 : $_ } split ',' ] } <DATA>;
jdporter
...porque es dificil estar guapo y blanco.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: reading data ...
by Ionizor (Pilgrim) on Dec 18, 2002 at 08:03 UTC |