ower_own_perl_wiki
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| ower_own_perl_wiki [2008/01/31 21:29] – typo fix cs243050 | ower_own_perl_wiki [2008/09/29 03:02] (current) – cs243081 | ||
|---|---|---|---|
| Line 53: | Line 53: | ||
| | LOCK_UN | | LOCK_UN | ||
| | LOCK_NB | | LOCK_NB | ||
| + | \\ | ||
| + | ---- | ||
| + | ===== Array manipulation: | ||
| + | |||
| + | So by now we know what an array looks like and maybe we've learned a few ways to manipulate it. Well, here's a trick to remove duplicate items in an array while preserving the item's order by using a hash. | ||
| + | |||
| + | Here's a couple of things you ought to know to fully understand the code: | ||
| + | |||
| + | * A hash is like an arrays except that they link a key to a value and the key is unique. | ||
| + | * $_ is a special perl variable known as the default or implicit variable. | ||
| + | |||
| + | <code perl> | ||
| + | @array1 = qw(one two one three four two); | ||
| + | |||
| + | foreach (@array1) { | ||
| + | push(@array2, | ||
| + | } | ||
| + | |||
| + | print " | ||
| + | </ | ||
| + | |||
| + | **output:** one two three four | ||
| + | |||
| + | The reason that we're using a hash here is to use its limitation (each key must be unique) to our advantage. | ||
| + | |||
| + | __Reference: | ||
ower_own_perl_wiki.1201814975.txt.gz · Last modified: by cs243050
