Sponsored Content
Top Forums Shell Programming and Scripting Problem using function in awk Post 302451084 by kristinu on Sunday 5th of September 2010 03:27:56 PM
Old 09-05-2010
Input arguments don't matter. They only return values from the function. I thought I would need to pass them through in order to return the values out from the function. I thought of using "return r1" for example. Calling srand() within the BEGIN statement should solve the problem of always getting the same values.
 

10 More Discussions You Might Find Interesting

1. Programming

Problem with aio_write() function

Hello, How to execute a call back function after aio_write() or aio_read() in Sun Solaris 5.7? I have filled the control block struct aiocb as follows: aio_sigevent.sigev_signo = SIGEV aio_sigevent.sigev_notify = SIGEV_THREAD Then I have filled the call back function in ... (0 Replies)
Discussion started by: hmurali
0 Replies

2. Shell Programming and Scripting

problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor ' BEGIN { printf("wgt:" wgt "\n"); printf("factor:" fac "\n"); total = sprintf("%.0f", wgt * fac); total2 = sprintf("%.0f", int(wgt * fac)); printf("total:" total "\n"); printf("total2:" total2 "\n"); } ' if vWeight=326.4 vFactor=100 the result... (2 Replies)
Discussion started by: qa.bingo
2 Replies

3. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

4. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

5. Shell Programming and Scripting

splice function problem

Hi All, I am using splice function in for loop to delete particular element from array with one condition. my $cnt=0; foreach my $elem (@result) { if (condition){ splice(@result, $cnt, 1);} else{ $cnt++;} } Now when in array, two elements comes sequentially with the... (3 Replies)
Discussion started by: gentleDean
3 Replies

6. UNIX for Advanced & Expert Users

AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report. Under linux I am scanning the... (5 Replies)
Discussion started by: Max Rebo
5 Replies

7. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

8. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

9. Shell Programming and Scripting

Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions. so heres my code: #!/bin/bash ##functions memory () { free -m } space () { df -h } ip () { (5 Replies)
Discussion started by: hawkfro12
5 Replies

10. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
Test::MockRandom(3pm)					User Contributed Perl Documentation				     Test::MockRandom(3pm)

NAME
Test::MockRandom - Replaces random number generation with non-random number generation VERSION
This documentation describes version 1.00. SYNOPSIS
# intercept rand in another package use Test::MockRandom 'Some::Other::Package'; use Some::Other::Package; # exports sub foo { return rand } srand(0.13); foo(); # returns 0.13 # using a seed list and "oneish" srand(0.23, 0.34, oneish() ); foo(); # returns 0.23 foo(); # returns 0.34 foo(); # returns a number just barely less than one foo(); # returns 0, as the seed array is empty # object-oriented, for use in the current package use Test::MockRandom (); my $nrng = Test::MockRandom->new(0.42, 0.23); $nrng->rand(); # returns 0.42 DESCRIPTION
This perhaps ridiculous-seeming module was created to test routines that manipulate random numbers by providing a known output from "rand". Given a list of seeds with "srand", it will return each in turn. After seeded random numbers are exhausted, it will always return 0. Seed numbers must be of a form that meets the expected output from "rand" as called with no arguments -- i.e. they must be between 0 (inclusive) and 1 (exclusive). In order to facilitate generating and testing a nearly-one number, this module exports the function "oneish", which returns a number just fractionally less than one. Depending on how this module is called with "use", it will export "rand" to a specified package (e.g. a class being tested) effectively overriding and intercepting calls in that package to the built-in "rand". It can also override "rand" in the current package or even globally. In all of these cases, it also exports "srand" and "oneish" to the current package in order to control the output of "rand". See "USAGE" for details. Alternatively, this module can be used to generate objects, with each object maintaining its own distinct seed array. USAGE
By default, Test::MockRandom does not export any functions. This still allows object-oriented use by calling "Test::MockRandom->new(@seeds)". In order for Test::MockRandom to be more useful, arguments must be provided during the call to "use". use Test::MockRandom 'Target::Package' The simplest way to intercept "rand" in another package is to provide the name(s) of the package(s) for interception as arguments in the "use" statement. This will export "rand" to the listed packages and will export "srand" and "oneish" to the current package to control the behavior of "rand". You must "use" Test::MockRandom before you "use" the target package. This is a typical case for testing a module that uses random numbers: use Test::More 'no_plan'; use Test::MockRandom 'Some::Package'; BEGIN { use_ok( Some::Package ) } # assume sub foo { return rand } was imported from Some::Package srand(0.5) is( foo(), 0.5, "is foo() 0.5?") # test gives "ok" If multiple package names are specified, "rand" will be exported to all of them. If you wish to export "rand" to the current package, simply provide "__PACKAGE__" as the parameter for "use", or "main" if importing to a script without a specified package. This can be part of a list provided to "use". All of the following idioms work: use Test::MockRandom qw( main Some::Package ); # Assumes a script use Test::MockRandom __PACKAGE__, 'Some::Package'; # The following doesn't interpolate __PACKAGE__ as above, but # Test::MockRandom will still DWIM and handle it correctly use Test::MockRandom qw( __PACKAGE__ Some::Package ); use Test::MockRandom %customized As an alternative to a package name as an argument to "use", Test::MockRandom will also accept a hash reference with a custom set of instructions for how to export functions: use Test::MockRandom { rand => [ Some::Package, {Another::Package => 'random'} ], srand => { Another::Package => 'seed' }, oneish => __PACKAGE__ }; The keys of the hash may be any of "rand", "srand", and "oneish". The values of the hash give instructions for where to export the symbol corresponding to the key. These are interpreted as follows, depending on their type: o String: a package to which Test::MockRandom will export the symbol o Hash Reference: the key is the package to which Test::MockRandom will export the symbol and the value is the name under which it will be exported o Array Reference: a list of strings or hash references which will be handled as above Test::MockRandom->export_rand_to() In order to intercept the built-in "rand" in another package, Test::MockRandom must export its own "rand" function to the target package before the target package is compiled, thus overriding calls to the built-in. The simple approach (described above) of providing the target package name in the "use Test::MockRandom" statement accomplishes this because "use" is equivalent to a "require" and "import" within a "BEGIN" block. To explicitly intercept "rand" in another package, you can also call "export_rand_to", but it must be enclosed in a "BEGIN" block of its own. The explicit form also support function aliasing just as with the custom approach with "use", described above: use Test::MockRandom; BEGIN {Test::MockRandom->export_rand_to('AnotherPackage'=>'random')} use AnotherPackage; This "BEGIN" block must not include a "use" statement for the package to be intercepted, or perl will compile the package to be intercepted before the "export_rand_to" function has a chance to execute and intercept calls to the built-in "rand". This is very important in testing. The "export_rand_to" call must be in a separate "BEGIN" block from a "use" or "use_ok" test, which should be enclosed in a "BEGIN" block of its own: use Test::More tests => 1; use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to( 'AnotherPackage' ); } BEGIN { use_ok( 'AnotherPackage' ); } Given these cautions, it's probably best to use either the simple or custom approach with "use", which does the right thing in most circumstances. Should additional explicit customization be necessary, Test::MockRandom also provides "export_srand_to" and "export_oneish_to". Overriding "rand" globally: use Test::MockRandom 'CORE::GLOBAL' This is just like intercepting "rand" in a package, except that you do it globally by overriding the built-in function in "CORE::GLOBAL". use Test::MockRandom 'CORE::GLOBAL'; # or BEGIN { Test::MockRandom->export_rand_to('CORE::GLOBAL') } You can always access the real, built-in "rand" by calling it explicitly as "CORE::rand". Intercepting "rand" in a package that also contains a "rand" function This is tricky as the order in which the symbol table is manipulated will lead to very different results. This can be done safely (maybe) if the module uses the same rand syntax/prototype as the system call but offers them up as method calls which resolve at run-time instead of compile time. In this case, you will need to do an explicit intercept (as above) but do it after importing the package. I.e.: use Test::MockRandom 'SomeRandPackage'; use SomeRandPackage; BEGIN { Test::MockRandom->export_rand_to('SomeRandPackage'); The first line is necessary to get "srand" and "oneish" exported to the current package. The second line will define a "sub rand" in "SomeRandPackage", overriding the results of the first line. The third line then re-overrides the "rand". You may see warnings about "rand" being redefined. Depending on how your "rand" is written and used, there is a good likelihood that this isn't going to do what you're expecting, no matter what. If your package that defines "rand" relies internally upon the system "CORE::GLOBAL::rand" function, then you may be best off overriding that instead. FUNCTIONS
"new" $obj = new( LIST OF SEEDS ); Returns a new Test::MockRandom object with the specified list of seeds. "srand" srand( LIST OF SEEDS ); $obj->srand( LIST OF SEEDS); If called as a bare function call or package method, sets the seed list for bare/package calls to "rand". If called as an object method, sets the seed list for that object only. "rand" $rv = rand(); $rv = $obj->rand(); $rv = rand(3); If called as a bare or package function, returns the next value from the package seed list. If called as an object method, returns the next value from the object seed list. If "rand" is called with a numeric argument, it follows the same behavior as the built-in function -- it multiplies the argument with the next value from the seed array (resulting in a random fractional value between 0 and the argument, just like the built-in). If the argument is 0, undef, or non-numeric, it is treated as if the argument is 1. Using this with an argument in testing may be complicated, as limits in floating point precision mean that direct numeric comparisons are not reliable. E.g. srand(1/3); rand(3); # does this return 1.0 or .999999999 etc. "oneish" srand( oneish() ); if ( rand() == oneish() ) { print "It's almost one." }; A utility function to return a nearly-one value. Equal to ( 2^32 - 1 ) / 2^32. Useful in "srand" and test functions. "export_rand_to" Test::MockRandom->export_rand_to( 'Some::Class' ); Test::MockRandom->export_rand_to( 'Some::Class' => 'random' ); This function exports "rand" into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name used in the other package as the alias to "rand": use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to( 'Some::Class' => 'random' ); } use Some::Class; srand (0.5); print Some::Class::random(); # prints 0.5 It can also be used to explicitly intercept "rand" after Test::MockRandom has been loaded. The effect of this function is highly dependent on when it is called in the compile cycle and should usually called from within a BEGIN block. See "USAGE" for details. Most users will not need this function. "export_srand_to" Test::MockRandom->export_srand_to( 'Some::Class' ); Test::MockRandom->export_srand_to( 'Some::Class' => 'seed' ); This function exports "srand" into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name to use in the other package as the alias for "srand". This function may be useful if another package wraps "srand": # In Some/Class.pm package Some::Class; sub seed { srand(shift) } sub foo { rand } # In a script use Test::MockRandom 'Some::Class'; BEGIN { Test::MockRandom->export_srand_to( 'Some::Class' ); } use Some::Class; seed(0.5); print foo(); # prints "0.5" The effect of this function is highly dependent on when it is called in the compile cycle and should usually be called from within a BEGIN block. See "USAGE" for details. Most users will not need this function. "export_oneish_to" Test::MockRandom->export_oneish_to( 'Some::Class' ); Test::MockRandom->export_oneish_to( 'Some::Class' => 'nearly_one' ); This function exports "oneish" into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name to use in the other package as the alias for "oneish". Since "oneish" is usually only used in a test script, this function is likely only necessary to alias "oneish" to some other name in the current package: use Test::MockRandom 'Some::Class'; BEGIN { Test::MockRandom->export_oneish_to( __PACKAGE__, "one" ); } use Some::Class; seed( one() ); print foo(); # prints a value very close to one The effect of this function is highly dependent on when it is called in the compile cycle and should usually be called from within a BEGIN block. See "USAGE" for details. Most users will not need this function. BUGS
Please report any bugs or feature requests using the CPAN Request Tracker. Bugs can be submitted through the web interface at <http://rt.cpan.org/Dist/Display.html?Queue=Test::MockRandom> When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. SEE ALSO
o Test::MockObject o Test::MockModule AUTHOR
David A. Golden (DAGOLDEN) COPYRIGHT AND LICENSE
Copyright (c) 2004-2007 by David A. Golden Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0> Files produced as output though the use of this software, shall not be considered Derivative Works, but shall be considered the original work of the Licensor. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. perl v5.10.0 2007-12-17 Test::MockRandom(3pm)
All times are GMT -4. The time now is 10:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy