subroutine

Externally declared (global) variable in Fortran

两盒软妹~` 提交于 2019-11-27 08:48:33
问题 I want to know if it's possible to declare a variable and have declaration carry over to another subroutine or program (hence become global) For example program main implicit none call mysub print *, x end program main subroutine mysub implicit none integer, parameter :: x = 1 end subroutine mysub Would print "1" Is this possible? I want to do this because a program I'm working on has large sets of variables that I would rather avoid copying unless necessary. 回答1: The most straightforward way

How to pass optional parameters to a Perl function?

◇◆丶佛笑我妖孽 提交于 2019-11-27 05:44:57
问题 I want to pass several parameters, one of which is optional, to a function. The only way to do it that I know is using a list (@) as a parameter. Thus, it contents nothing or 1 element (will never be undef), so that I can use the following code: sub someFunction($$@) { my ( $oblig_param1, $oblig_param2, $option_param ) = @_; ... } This code works, but I feel that maybe it's not the best workaround. Are there any other ways to do it? Thank you. 回答1: You can use a semicolon in the prototype to

How to pass subroutine names as arguments in Fortran?

社会主义新天地 提交于 2019-11-27 04:39:18
问题 What is the syntax for passing subroutine names as arguments? Schematically: . . call action ( mySubX ( argA, argB ) ) . . subroutine action ( whichSub ( argA, argB ) ) ... call subroutine whichSub ( argA, argB ) ... end subroutine action The goal is to have call subroutine whichSub ( argA, argB ) act as call subroutine mySubX ( argA, argB ) . My preference is to avoid avoid passing a switch parameter and then use SELECT CASE. 回答1: It is call action(mySubX) provided action looks as subroutine

How can I selectively access elements returned by a Perl subroutine?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 03:56:04
问题 Say a Perl subroutine returns an array: sub arrayoutput { ...some code... return @somearray; } I want to access only a specific array element from this, say the first. So I could do: @temparray=arrayoutput(argument); and then refer to $temparray[0] . But this sort of short reference doesn't work: $arrayoutput(some argument)[0] . I am used to Python and new to Perl, so I'm still looking for some short, intuitive, python-like way ( a=arrayoutput(some argument)[0] ) to get this value. My Perl

how to compile multi-folder Fortran Project having interfaces, modules and subroutines

断了今生、忘了曾经 提交于 2019-11-27 03:33:25
问题 I am new to Fortran. I am working on a research project where I am using an open source project that has several files distributed in multiple folders. i found the dependency of each programs but could not figure out how to compile them. I have source code distributed in three folders. a)modules b)interfaces c)subroutines I would like to run a program named as 'Main.f90' in subroutines folder, this program has dependency of source codes from modules and interfaces folders. I am using eclipse

How can I write a Perl script to extract the source code of each subroutine in a Perl package?

只谈情不闲聊 提交于 2019-11-27 03:32:59
问题 Given a Perl package Foo.pm, e.g. package Foo; use strict; sub bar { # some code here } sub baz { # more code here } 1; How can I write a script to extract the textual source code for each sub, resulting in a hash: $VAR1 = { 'bar' => 'sub bar { # some code here }', 'baz' => 'sub baz { # more code here }' }; I'd like to have the text exactly as it appears in the package, whitespace and all. Thanks. 回答1: PPI is kind of a pain to work with at the very first; the documentation is not good at

What does the function declaration “sub function($$)” mean?

不打扰是莪最后的温柔 提交于 2019-11-27 02:15:45
I have been using Perl for some time, but today I came across this code: sub function1($$) { //snip } What does this mean in Perl? Jonathan Leffler It is a function with a prototype that takes two scalar arguments. There are strong arguments for not actually using Perl prototypes in general - as noted in the comments below. The strongest argument is probably: Far More Than Everything You've Ever Wanted to Know about Prototypes in Perl There's a discussion on StackOverflow from 2008: SO 297034 There's a possible replacement in the MooseX::Method::Signatures module. jrockway As the other answer

Why would I use Perl anonymous subroutines instead of a named one?

谁说胖子不能爱 提交于 2019-11-27 01:35:14
问题 I'm just curious why one would choose to use an anonymous subroutine, versus a named one, in Perl. Thanks. 回答1: You can store anonymous subs in arrays, hashes and scalars. You can build them at runtime You can pass them as arguments to other functions. You get to keep variables in the surrounding scope. The last point is probably the most important, because it's often the most unexpected facet of named vs. anonymous subroutines in Perl. Example: sub outer { my $a = 123; sub inner { print $a,

Passing strings for execution in Fortran subroutines

谁都会走 提交于 2019-11-26 21:44:20
问题 In the following subroutine I would like to pass a string variables named str . If it is 'poly' , 'gaus' , 'slat' , then it has a predefined action ( fval = see code below ). I would like to have the user specify a function to use and pass that as a string variable. That is ... If str = '3*cos(i*t)' , then i would like to have fval be equal to 3*cos(i*t) . How can I get Fortran to interpret the string entered as a command to be executed by Fortran? subroutine f(fval, i, t, str) implicit none

Module calling an external procedure with implicit interface

拟墨画扇 提交于 2019-11-26 21:43:56
问题 The following code, combining module procedures and external procedures : module module_dummy implicit none contains subroutine foo(a) real, intent(inout) :: a(:) call bar(a) end subroutine foo end module module_dummy program main use module_dummy implicit none integer, parameter :: nelems = 100000000 real, allocatable :: a(:) allocate( a(nelems) ) a = 0.0 call foo(a) print *, a(1:10) deallocate(a) end program main subroutine bar(a) implicit none real, intent(inout) :: a(:) a = 1.0 end