Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re^3: Replacing values in an array by muba
in thread Replacing values in an array by tonto

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Corion]: choroba: I'm still using spod5 as my pod-to-ooxml- converter didn't really get off the ground so far
    [choroba]: Slidy is basically HTML, so I'm not sure I really fly high :)
    choroba should make his presentation scaffolding public
    [Corion]: choroba: spod5 converts pod to S5 HTML, so it's also still basic. I find the lack of animations (in the sense of "highlight this", "highlight that" in code) somewhat tedious as I do it with rendered PNGs
    [Corion]: I haven't found a good way to include/use the source SVGs I use for creating the PNGs directly as animations
    [ambrus]: Presentations come in many different shapes, and so slides do as well.
    [Corion]: Doing that in Powerpoint or Ooxml would be nice(r) but I'm much quicker doing the outline of a presentation and the code as Pod
    [Corion]: Hurrr - on a machine that is behaving weirdly, I have two processes CROND running. I guess that is the source of unattended jobs sometimes not finding their files anymore...
    [ambrus]: Most of the time if I make slides, they're just a formatted document with a medium level of formality (between a well written article and an informal draft), with usually the page breaks chosen carefully and possibly some content repeated between pages.

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (10)
    As of 2017-09-26 10:04 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (293 votes). Check out past polls.