Sponsored Content
Top Forums Shell Programming and Scripting Understanding an example of perl map() function Post 303003550 by Corona688 on Friday 15th of September 2017 02:58:03 PM
Old 09-15-2017
Yes. Perl allows you to call things without the () if you really want to, and they did so here. How it would with them is:

Code:
map( {code block}, @ARGV)

So @ARGV is the input array, and {code block} is what it does to every element of the array in turn. It's given @ARGV[N] as its argument, and returns whatever you want to transform it into.

I'm not sure that's valid syntax for perl in general - shoving entire code blocks wherever - especially since that code block isn't executed immediately, but repeatedly called by map(). This seems like special behavior.

The steps in the { } block are:
  • Match $_ (i.e. @ARGV[N]) against /(.*)\//
  • Return $1, i.e. the bracketed section matched by the regex
  • Assign the returned value to output[N] ( implied, done internally by map() )

Once every element is parsed, it's returned as a list into join().
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl map doubt

Hello , Please can someone tell me what exactly happens when the below filehandler is chomped into an array and later mapped. $lcpLog="logcopy\@".getTimestamp."\log"; open CFg ,"< $lcpcfg"; chomp(@cfg = <CFG>); close CFG; @cfg=grep { $_ ne ' ' } map { lc + (split /\s*\/\//) }... (0 Replies)
Discussion started by: rmv
0 Replies

2. Shell Programming and Scripting

Help needed for understanding a function

There is a function called start: start() { echo -n $"Sending Startup Email: " echo "${RESTARTBODY}" | mutt -s "${RESTARTSUBJECT}" ${EMAIL} RETVAL=$? if ; then touch ${LOCKFILE} success else failure fi echo return ${RETVAL} } Can anyone explain what the bold part of the... (3 Replies)
Discussion started by: proactiveaditya
3 Replies

3. Shell Programming and Scripting

Grep and map in perl

Hi guys, I'm trying to learn grep and map and having a little problem. Let's say I have a file which contains: Apple: abcdcabdadddbac I want to replace any combinations of three of abcd, thus when I do this: print grep {s/{3}/X/g} <F>; # will do the subtitution fine, output XXXX ... (1 Reply)
Discussion started by: new bie
1 Replies

4. Shell Programming and Scripting

How to map the values of an array in perl?

Hi, I have 2 arrays: @names=qw(amith veena chaitra); @files=qw(file.txt file1.txt file3.txt); There is one to one relationship between names and files. There needs to be mapping created between names and files. The output should be like this: amith --> file.txt veena --->... (3 Replies)
Discussion started by: vanitham
3 Replies

5. Shell Programming and Scripting

Help understanding some Perl code.

Well, I found myself trying to fix some Perl code (Ive never done any Perl in my life) and I pinpointed the place where the bug could be. But to be sure I have to know what does a few line of code mean: $files_lim =~ (/^\d*$/) $files_lim =~ (/^\d*h$/) $files_age =~ s/h// The code where... (2 Replies)
Discussion started by: RedSpyder
2 Replies

6. Shell Programming and Scripting

understanding thread in perl

Hi all, I am trying to build threads which will go to localhost and list the files in given folder. #!/usr/bin/perl use threads; my $t1 = threads->new(\&sub1, 1); my $t2 = threads->new(\&sub2, 2); push(@threads,$t1); push(@threads,$t2); foreach... (5 Replies)
Discussion started by: PranavEcstasy
5 Replies

7. Shell Programming and Scripting

Looping in Perl based on defined keys in Map

Hello All, I am writing the below script where it will connect to database and returns the results. #!/sw/gcm/perl510/bin/perl use SybaseC; &openConnection; &loadvalues; sub openConnection { $dbproc = new SybaseC(SYDB}, $ENV{DBDFLTUSR}, $ENV{DBDFLTPWD}); if... (2 Replies)
Discussion started by: filter
2 Replies

8. Shell Programming and Scripting

Need help understanding a function

Hello, I recently started going in depth with the shell, so I started learning from Linux Shell Scripting CookBook, 2nd edition. I am at the first chapter atm, and the author tells to define a function in the ~/.bashrc. The function is below. prepend() { && eval $1=\"$2':'\$$1\" && export... (1 Reply)
Discussion started by: Vaseer
1 Replies

9. Shell Programming and Scripting

Perl combine multiple map statements

I have a file like file. file.TODAY.THISYEAR file.TODAY.LASTYEARI want to substitute the words in caps with their actual values so that output should look like file.140805 file.140805.2014 file.140805.2013For this I am reading the file line bye line in an array and using multiple map... (1 Reply)
Discussion started by: sam05121988
1 Replies

10. Shell Programming and Scripting

Problem with (Understanding) function

I have this code #!/bin/bash LZ () { RETVAL="\n$(date +%Y-%m-%d_%H-%M-%S) --- " return RETVAL } echo -e $LZ"Test" sleep 3 echo -e $LZ"Test" which I want to use to make logentrys on my NAS. I expect of this code that there would be output like 2017-03-07_11-00-00 --- Test (4 Replies)
Discussion started by: matrois
4 Replies
Dpkg::Conf(3)							   libdpkg-perl 						     Dpkg::Conf(3)

NAME
Dpkg::Conf - parse dpkg configuration files DESCRIPTION
The Dpkg::Conf object can be used to read options from a configuration file. It can exports an array that can then be parsed exactly like @ARGV. FUNCTIONS
my $conf = Dpkg::Conf->new(%opts) Create a new Dpkg::Conf object. Some options can be set through %opts: if allow_short evaluates to true (it defaults to false), then short options are allowed in the configuration file, they should be prepended with a single dash. @$conf @options = $conf->get_options() Returns the list of options that can be parsed like @ARGV. $conf->load($file) Read options from a file. Return the number of options parsed. $conf->parse($fh) Parse options from a file handle. Return the number of options parsed. $conf->filter(remove => $rmfunc) $conf->filter(keep => $keepfunc) Filter the list of options, either removing or keeping all those that return true when &$rmfunc($option) or &keepfunc($option) is called. $string = $conf->output($fh) Write the options in the given filehandle (if defined) and return a string representation of the content (that would be) written. "$conf" Return a string representation of the content. $conf->save($file) Save the options in a file. AUTHOR
Raphael Hertzog <hertzog@debian.org>. 1.16.0.3 2012-04-17 Dpkg::Conf(3)
All times are GMT -4. The time now is 07:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy