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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |