Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Replacing values in an array

by BrowserUk (Pope)
on Jan 26, 2013 at 23:43 UTC ( #1015546=note: print w/ replies, xml ) Need Help??


in reply to Replacing values in an array

Somewhat simpler version:

my( $n, %h ) = -1; my @a = map{ $h{$_} //= $n+=2 } qw[ M94202 M94150 M94297 M94150 M94161 + M94161 M94162 ];; print @a;; 1 3 5 3 7 7 9

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Replacing values in an array
Download Code
Re^2: Replacing values in an array
by tonto (Pilgrim) on Jan 26, 2013 at 23:54 UTC
    Brilliant!
    Now I have three elegant solutions and a reason to study "map"!
    Thank you!
    -tonto

      map and grep can be an intimidating functions, but quite useful once you understand them.

      Let's consider grep first, since this one is a little simpler to grasp — or at least, that was my experience. grep takes two arguments, the second being a list of elements to work with, the first being the work that you want to have done on that list. You can specify that as a BLOCK, a code reference, or just the name of a function. grep then loops over the list, aliasing $_ to each element in turn, and calls the given piece of code. Then it returns every element for which the code returned a true value.

      my @numbers = (0, 0.5, 1, 1.5, 2, 2.5); # Calling grep with a BLOCK: my @integers = grep {int($_) == $_} @numbers; print join(", ", @integers), " are integers.\n"; # Calling grep with a function name: my @basket = ("apple", undef, undef, undef, "banana", "cherry", undef, + "date"); print "The number of elements in \@basket is ", scalar(@basket), "\n"; my @basket_1 = grep defined, @basket; print "The number of *defined* elements in \@basket is ", scalar(@bask +et_1), "\n";

      See what happens? grep returns the elements of the list you gave it, for which the piece of code returns true. The non-grep equivalents would be:

      my @numbers = (0, 0.5, 1, 1.5, 2, 2.5); # Calling grep with a BLOCK: my @integers; for (@numbers) { push @integers, $_ if int($_) == $_; } print join(", ", @integers), " are integers.\n"; # Calling grep with a function name: my @basket = ("apple", undef, undef, undef, "banana", "cherry", undef, + "date"); print "The number of elements in \@basket is ", scalar(@basket), "\n"; my @basket_1; for (@basket) { push @basket_1, $_ if defined; } print "The number of *defined* elements in \@basket is ", scalar(@bask +et_1), "\n";

      Now, map is pretty similar, except that it allows you to change the elements:

      my @numbers = 1..10; my @times_ten = map { $_ * 10 } @numbers; print join(", ", @times_ten), "\n";

      Of course, these are just the basics — the range of things you can do with them is astonishing. I hope this helps you along.

Re^2: Replacing values in an array
by eyepopslikeamosquito (Canon) on Jan 27, 2013 at 01:31 UTC

    Using for instead of map:

    use strict; use warnings; use Data::Dumper; my @a = ("M94202", "M94150", "M94297", "M94150", "M94161", "M94161", " +M94162"); my ( $n, %h ) = -1; $_ = $h{$_} //= $n+=2 for @a; print Dumper \@a;

    I suppose if you were playing golf, without use strict, you might write:

    my ( $n, %h ) = -1; $_ = $h{$_} //= $n+=2 for @a;
    in one-line as:
    $_=$h{$_}//=$}+=2-!$}for@a;
    (/me ducks)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-08-30 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls