UPDATED

I'm trying to do with "map" what does this code using a loop

#!/usr/local/bin perl5.14.2 + use warnings; use strict; use File::Basename; my @extens = qw(.zip .dmg .tar.gz .pl .pl~ .cgi .mp3 .txt); my @ppal; my $i = 0; foreach my $arch (@ARGV ){ my ($name,$path,$suffix) = fileparse($arch,@extens); $ppal[$i] = []; push @{$ppal[$i]},($path,$name,$suffix); $i++; } foreach my $elem (@ppal){ print "Path:\t@{$elem}[0]\tArchivo:\t@{$elem}[1]\tExtens:\t@{$elem +}[2]\n"; }

Although as I say below, it is a simple exercise. I do not know whether it makes sense to map this

As I said in previous comments, I am learning to use map, (which means that, right now, I use it even, probably, in those circumstances should not be used). I'm following the book "Intermediate Perl", which in its second chapter discusses the module "File:: basename".Great!, (I said to myself), I will map it.

The most obvious solution for me is to use a loop "foreach" but never have consented that the evidence is to be preferred to my whims.

This code works correctly (although with some problems with file names that contain a "space". Later I'll look how that settle it the module):

#!/usr/local/bin perl5.14.2 + use File::Basename; my @sufijos = qw(.dmg .tar.gz .rar .zip); foreach $arch (@ARGV) { + $name = basename( $arch, @sufijos); print "Nombre: $name\n"; }

I have begun to try something like this:

#!/usr/local/bin perl5.14.2 + use File::Basename; my @sufijos = qw(.dmg .tar.gz .rar .zip); sub parsear { my $input = shift; $name = basename ( $input, @sufijos ); } print map &parsear($_)."\n" , @ARGV;

In "@ ​​ARGV" stores the output of a command 'ls'

This works as expected ... But ... What if I want to parse a full path by using the "fileparse" that will output an array? I guess the solution is the use of references. You would want to explain how to do ... Not so much the solution as a little explanation to understand the "hows" and "whys".

Do not think I'm obsessed with the "map" is just to learn.

PS: I leave you, it seems that my implementation of map for the microwave is giving problems. XD


In reply to Complex structures and function "map" by nando

Title:
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, details, 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, summary, 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.