Re: Can someone Explain 2 lines of code
by sauoq (Abbot) on Oct 28, 2003 at 20:09 UTC
|
I know it's removing the duplicate values in an array
No, it isn't.
It is creating a hash called %count where the keys are the stringified values in the array @Clec_id and the values are the number of occurrences of each array value. Then it is printing the keys and values of that hash.
It may be true that keys %count contains the values of the original array with duplicates removed but not guaranteed. For instance, if the array contains references, they won't exist as keys in the hash; only their stringified representation will. Also, the keys of the hash are unlikely to be returned in the same order as the values of the array.
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
|
I am sorry you are right, I was looking at another part of the code , and yes this one is calculating the number of times each value has occured in an array .. I am sorry about my totally mis typed description .. thanks again for explaninig it ...
| [reply] |
|
|
An apology isn't at all necessary. You already said you didn't understand it. :-)
If you still don't understand it, can you be more specific about what you don't understand? You seem to understand arrays... do you understand hashes? The post-increment? The keys function? The loop? The $_ variable?
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
|
Update:
So you were adding this at the time when I was typing ;-) and obviously yours has a richer content.
Original:
Just to add a little bit on top of your comment. A slight change to the code, he can actually use keys %blah as the array with all duplicates removed.
| [reply] |
|
|
I misread the reply, so I put too fine a point on it...
Not to point too fine a point on it...
Then it is printing the keys and values of that hash.
...which in this case "values" is the number of times each key was seen.
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] |
Re: Can someone Explain 2 lines of code
by hardburn (Abbot) on Oct 28, 2003 at 20:13 UTC
|
foreach my $data (@Clec_id) {
$count{$data}++;
}
Every time the same peice of data appears in the array, that index key is incremented. Thus, if the string 'foo' appears three times in the array, then $count{foo} will be three after the loop runs.
The second line is simply dumping the contents of the hash to STDOUT using a similar construct to the line above it.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
:(){ :|:&};:
Note: All code is untested, unless otherwise stated
| [reply] [d/l] [select] |
Re: Can someone Explain 2 lines of code
by Zaxo (Archbishop) on Oct 28, 2003 at 20:19 UTC
|
There is a hash called %count, which is produced when it is first referred to by perl. The first of those lines increments a value in the hash keyed by $_, the current element of @Clec_id in the for loop. The property of uniquifying the array elements comes from the way hash keys work. Incrementing the value counts the number of times a key appears in @Clec_id
In the second line, there is a loop over the unique elements generated by keys %count. In that loop, $_ comes from those keys. Each is printed with a fat arrow and its count on a separate line.
| [reply] |
Re: Can someone Explain 2 lines of code
by jonadab (Parson) on Oct 29, 2003 at 01:31 UTC
|
$count{$_}++ for @Clec_id;
print "$_ => $count{$_}\n" for keys %count;
can someone please explain
Well, I got here too late and other people had already
explained. So instead, I decided to play games with
it, like getting it onto one line and getting rid of
the well-named variables. (Honestly, naming a variable
"count" when it's perfectly obvious that it's a counting
variable... if you're not careful, people will be able
to read your code...) So, um, this may
not be helpful or answer your question, but it was fun:
print"$_ => $}{$_}\n"for grep{!$!{$_}++}map{$}{$_}++;$_}@Clec_id;
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
you kept the @Clec_id variable
There were two reasons to keep that, but the most
important reason was that it was presumably
given its value in earlier code, so I'd have to copy
from it anyway, not having access to the rest of
the code to change it. (The other reason to keep
it, of course, is that there was no compelling reason
to do otherwise.)
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
| [reply] [d/l] |
Re: Can someone Explain 2 lines of code
by TomDLux (Vicar) on Oct 29, 2003 at 04:49 UTC
|
| [reply] |
|
|
Why? Because it's blatant copyright infringement, of course! You must immediately take legal action against this great injustice.
But really. There's only so many ways to skin that cat (not to mention it's a fairly common idiom). I'm sure I have code somewhere that's 90% the same as the answer you linked to as well.
| [reply] |