Sponsored Content
Top Forums UNIX for Dummies Questions & Answers "Sliding window" with variables Post 302619099 by agama on Thursday 5th of April 2012 12:28:05 AM
Old 04-05-2012
Using arrays would be one way:

Code:
#!/usr/bin/env ksh
# add a value to the array
function add_value
{
    typeset i=0

    i=${#values[@]}             # current length
    if (( $i >= $nvalues ))     # cap at nvalues
    then
        i=$(( $nvalues - 1 ))
    fi
    for (( ; i > 0; i-- ))          # shift values up in the array
    do
        values[$i]=${values[$(($i-1))]}
    done

    values[0]=$1            # add the new value
}

# compute the mean
function mean
{
    typeset i=0
    typeset sum=0

    if (( ${#values[@]} < 1 ))   # prevent divide by zero accidents
    then
        echo 0
        return
    fi

    for (( i = 0; i < ${#values[@]}; i++ ))
    do
        sum=$(( ${values[$i]} + sum ))
    done

    echo $(( sum / ${#values[@]} ))
}

nvalues=5               # max number of values to maintain

add_value 25            # add values to the list
add_value 2
add_value 23
mean                    # compute mean at any time if needed
add_value 42   
add_value 43
add_value 44

mean                    # compute mean
exit


Should work in either bash or Kshell.
This User Gave Thanks to agama For This Post:
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Windows & DOS: Issues & Discussions

"Striping" the background of an Rxvt/Urxvt window in Cygwin

To get this: https://www.unix.com/members/silversleevesx-albums-incidental-shot-glass-picture127-termshot-rxvt-rootless.png out of Cygwin's rxvt, you have to tweak your /cygwin/etc/x11/app-defaults/rxvt file, which is here:... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

3. Shell Programming and Scripting

Question about special variables: "-" and "$_"

both ksh/bash support this 2 special variables, Is there any document for reference? 1) "-" is $OLDPWD 2) "$_" is last argument of previous command. (4 Replies)
Discussion started by: honglus
4 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Solaris

Trap signal on Window Manager "X" button clicked?

Well, my first post... thanks in advance! Can applications be notified of the X Window close (with "X" button) so the signal handler can run a cleanup process method? About the app: built with GNU C/C++ on Solaris 10, with WxWidgets. It is launched by a shell script as a background task. The... (2 Replies)
Discussion started by: HandsOGold
2 Replies

6. Web Development

Attaching " Thank You " pop-up window in php form mail code

Dear Sir, I have got a php mail form code. Using this code, I want to use the JavaScript for " Thank You " pop-up window in the php form mail code. Where do I add the pop-up JavaScript for "thank you" ? My code is as follows: <?php //--------------------------Set these... (1 Reply)
Discussion started by: swapan
1 Replies

7. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

8. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies
Config::MVP::Assembler(3pm)				User Contributed Perl Documentation			       Config::MVP::Assembler(3pm)

NAME
Config::MVP::Assembler - multivalue-property config-loading state machine VERSION
version 2.200002 DESCRIPTION
First, you should probably read the example of using Config::MVP. If you already know how it works, keep going. Config::MVP::Assembler is a helper for constructing a Config::MVP::Sequence object. It's a very simple state machine that lets you signal what kind of events you've encountered while reading configuration. ATTRIBUTES
sequence_class This attribute stores the name of the class to be used for the assembler's sequence. It defaults to Config::MVP::Sequence. section_class This attribute stores the name of the class to be used for sections created by the assembler. It defaults to Config::MVP::Section. sequence This is the sequence that the assembler is assembling. It defaults to a new instance of the assembler's "sequence_class". METHODS
begin_section $assembler->begin_section($package_moniker, $name); $assembler->begin_section($package_moniker); $assembler->begin_section( $package ); This method tells the assembler that it should begin work on a new section with the given identifier. If it is already working on a section, an error will be raised. See "change_section" for a method to begin a new section, ending the current one if needed. The package moniker is expanded by the "expand_package" method. The name, if not given, defaults to the package moniker. These data are used to create a new section and the section is added to the end of the sequence. If the package argument is a reference, it is used as the literal value for the package, and no expansion is performed. If it is a reference to undef, a section with no package is created. end_section $assembler->end_section; This ends the current section. If there is no current section, an exception is raised. change_section $assembler->change_section($package_moniker, $name); $assembler->change_section($package_moniker); This method calls "begin_section", first calling "end_section" if needed. add_value $assembler->add_value( $name => $value ); This method tells the assembler that it has encountered a named value and should add it to the current section. If there is no current section, an exception is raised. (If this is not the first time we've seen the name in the section and it's not a multivalue property, the section class will raise an exception on its own.) expand_package This method is passed a short identifier for a package and is expected to return the full name of the module to load and package to interrogate. By default it simply returns the name it was passed, meaning that package names must be given whole to the "change_section" method. current_section This returns the section object onto which the assembler is currently adding values. If no section has yet been created, this method will return false. TYPICAL USE
my $assembler = Config::MVP::Assembler->new; # Maybe you want a starting section: my $starting_section = $assembler->section_class->new({ name => '_' }); $assembler->sequence->add_section($section_starting); # We'll add some values, which will go to the starting section: $assembler->add_value(x => 10); $assembler->add_value(y => 20); # Change to a new section... $assembler->change_section($moniker); # ...and add values to that section. $assembler->add_value(x => 100); $assembler->add_value(y => 200); The code above creates an assembler and populates it step by step. In the end, to get values, you could do something like this: my @output; for my $section ($assembler->sequence->sections) { push @output, [ $section->name, $section->package, $section->payload ]; } When changing sections, the given section "moniker" is used for the new section name. The result of passing that moniker to the assembler's "expand_package" method is used as the section's package name. (By default, this method does nothing.) The new section's "multivalue_args" and "aliases" are determined by calling the "mvp_multivalue_args" and "mvp_aliases" methods on the package. AUTHOR
Ricardo Signes <rjbs@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Ricardo Signes. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-03-16 Config::MVP::Assembler(3pm)
All times are GMT -4. The time now is 08:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy