*scratches head* That's a little obfu. Could you break down what it does in steps? This looks interesting.
| [reply] |
Sure, no prob.
First, this:
my @data = map { chomp; [ split ',' ] } <DATA>;
Can reformatted as
my @data =
map
{
chomp;
[
split ','
]
}
<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;
[
map
{
/"(.*)"/
? $1
: $_
}
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.
jdporter ...porque es dificil estar guapo y blanco. | [reply] [d/l] [select] |
Ah! Thank you. I always wondered about map. Now that I know what map does, what the rest of the code does is straightforward. Now I can probably decode half those sigs I wondered about too. :)
| [reply] |
For Ionizer, to "un"obfuscate this a little, here's a step by step approach that basically does the same thing in a different way:
my @data = ();
while (<DATA>) { # read one line
chomp; # remove end-of-line character(s)
s/\"//g; # remove double quotes if present
push @data, [ $_ ]; # Create an anonymous array
# *reference* with the current
# line, and push that reference
# onto the @data array.
}
I was trying to explain the obfuscated statement made by jdporter, but it was too involved.
HTH. | [reply] [d/l] |
Thanks. Now I'll make a few comments and corrections.
my @data = ();
That assignment isn't necessary. Arrays start out empty!
s/\"//g;
That backwhack isn't necessary. Quote marks are literal in any quoted string that uses some other delimiter.
push @data, [ $_ ];
That isn't equivalent and won't work for the OP's purpose.
You still need to split the string on commas.
You could do it this way:
my @a = split /,/;
push @data, \@a;
jdporter ...porque es dificil estar guapo y blanco. | [reply] [d/l] [select] |
Thanks for the corrections!
One question:
push @data, [ $_ ];
That isn't equivalent and won't work for the OP's purpose.
You still need to split the string on commas.
Wasn't the OP looking for a solution that would put each *record* from <DATA> into an anonymous array reference, and then put that anonymous array ref into the @data array? If that is the goal, then why would you need to split the record on comma's? | [reply] [d/l] |