Sponsored Content
Full Discussion: ksh insert element in array
Top Forums Shell Programming and Scripting ksh insert element in array Post 302628417 by gary_w on Monday 23rd of April 2012 12:38:38 PM
Old 04-23-2012
My thought was to shift the elements of the array over by one until we hit the one that needs to be zero. I assumed the sixth element based on the index as arrays are zero-based. You may have to adjust accordingly:
Code:
$ cat x
#!/bin/ksh

## Create the starting array with 11 elements.  Arrays are
## zero-based, so they would be elements 0-10.
set -A arrayA 1 2 3 4 5 6 7 8 9 10 11

## arrayA_pointer starts out equal to the number of elements in the array
## (11), thus points 1 past the end (arrayA[11] does not exist yet).
integer arrayA_pointer=$((${#arrayA[@]}))

integer ctr=0
typeset -ir INSERT_POINT=6

## Show the starting array.
print "Before: [${arrayA[@]}]"

## Loop backwards through the elements, shifting them to the value
## of the one before it, until we come to the element that needs
## to be set to 0.
while (( $arrayA_pointer > $INSERT_POINT )); do
  arrayA[${arrayA_pointer}]=${arrayA[${arrayA_pointer}-1]}
  (( arrayA_pointer=arrayA_pointer-1 ))
done

## We hit the element that needs to be set to 0.
arrayA[$arrayA_pointer]=0

## Loop through the processed array, printing out each element.
printf "After: "
while (( ${ctr} < ${#arrayA[@]} )); do
  if (( ${ctr} + 1  == ${#arrayA[@]} )); then
    printf "%s\n" ${arrayA[$ctr]}
  else
    printf "%s, " ${arrayA[$ctr]}
  fi
  (( ctr=ctr+1 ))
done

exit 0
$ x
Before: [1 2 3 4 5 6 7 8 9 10 11]
After: 1, 2, 3, 4, 5, 6, 0, 7, 8, 9, 10, 11
$


Last edited by gary_w; 04-23-2012 at 02:38 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding array element in KSH

All, I would like to add the first 10 elements of an array. Here is how I am doing it now (only included first few add ops): #!/usr/bin/ksh ###Grab the array values out of a file### TOTAL=`awk '/time/' /tmp/file.out | awk '{print $4}'` set -A times $TOTAL SUM=$((${times} + times... (3 Replies)
Discussion started by: Shoeless_Mike
3 Replies

2. Shell Programming and Scripting

Shift array element

I want to delete and 0th element of array in shell scrpit and also shift all others to one level up. (2 Replies)
Discussion started by: darshakraut
2 Replies

3. Shell Programming and Scripting

remove an element from array

I need to remove an element from the below array variable TABLENAME. #!/bin/ksh set -A TABLENAME "mirf roxar keke mirs" echo "the array is ${TABLENAME}" If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light. echo "Modified... (3 Replies)
Discussion started by: michaelrozar17
3 Replies

4. Shell Programming and Scripting

Problem to initialize ksh array when first element includes hyphen

Hi I'm trying to create an array with variable including hyphen but ksh refuses the first element set -A allArgs set +A allArgs ${allArgs} -all set +A allArgs ${allArgs} -date set +A allArgs ${allArgs} test ./test.ksh: -all: bad option(s) It happens only when first element is like... (4 Replies)
Discussion started by: gdan2000
4 Replies

5. Emergency UNIX and Linux Support

Assigning zero to element of ksh array.

set -A matched #find referenced files. for i in ${file_names_html} do counter_j=0 for j in ${file_names_minus_index} do match=`cat $i | grep... (1 Reply)
Discussion started by: robin_simple
1 Replies

6. Shell Programming and Scripting

Not able to call an element from an array in ksh

Hi, I have: # Initialize variables #!/usr/bin/ksh FILENM=$1 INDEX=0 # read filename echo "You are working with the Config file: $FILENM" while read line do echo $line data=$line ((INDEX=INDEX+1)) done <"$FILENM" (3 Replies)
Discussion started by: Marc G
3 Replies

7. Shell Programming and Scripting

How to insert an array element within regex?

Hello to all, I'm trying to separate the string "str" using a regex within match function. The substrings that I want to separate, begin with 22, 23, 24 or 25 and followed by 12 or 14 characters. And I want to replace 22 with MJS, 23 with UYT, 24 with WER and 25 with PIL. For this string... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

8. Shell Programming and Scripting

File names as array element in ksh

Hi, I have an ksh array(ARR). the elements to the array are file names. i need to go to each file in the array and manipulate the records. for name in ${files}; do ---this loop is for all the file names in the array for i in $(wc -l < $name); do --this loop is for all the records in... (20 Replies)
Discussion started by: usrrenny
20 Replies

9. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

10. Shell Programming and Scripting

How to insert a CSV within xml element tag using Python?

Hi Team, I have a CSV file which I have to read through and needs to insert the content within an XML file using Python ONLY ( as most of the code base we have in python only). I managed to find the first part, missing how to insert to XML under "specific" tags. cat input.csv... (0 Replies)
Discussion started by: panyam
0 Replies
PMC(9)							   BSD Kernel Developer's Manual						    PMC(9)

NAME
pmc, pmc_get_num_counters, pmc_get_counter_type, pmc_save_context, pmc_restore_context, pmc_enable_counter, pmc_disable_counter, pmc_counter_isrunning, pmc_counter_isconfigured, pmc_configure_counter, pmc_get_counter_value, pmc_accumulate, pmc_alloc_kernel_counter, pmc_free_kernel_counter, pmc_start_profiling, pmc_stop_profiling, PMC_ENABLED -- Hardware Performance Monitoring Interface SYNOPSIS
#include <sys/pmc.h> int pmc_get_num_counters(void); int pmc_get_counter_type(int ctr); void pmc_save_context(struct lwp *l); void pmc_restore_context(struct lwp *l); int pmc_enable_counter(struct lwp *l, int ctr); int pmc_disable_counter(struct lwp *l, int ctr); int pmc_counter_isrunning(struct lwp *l, int ctr); int pmc_counter_isconfigured(struct lwp *l, int ctr); int pmc_configure_counter(struct lwp *l, int ctr, struct pmc_counter_cfg *cfg); int pmc_get_counter_value(struct lwp *l, int ctr, int flags, uint64_t *pval); int pmc_accumulate(struct lwp *l_parent, struct lwp *l_exiting); int pmc_alloc_kernel_counter(int ctr, struct pmc_counter_cfg *cfg); int pmc_free_kernel_counter(int ctr); int pmc_start_profiling(int ctr, struct pmc_counter_cfg *cfg); int pmc_stop_profiling(int ctr); int PMC_ENABLED(struct lwp *l); DESCRIPTION
Provides a machine-independent interface to the hardware performance counters which are available on several CPU families. The capabilities of these counters vary from CPU to CPU, but they basically count hardware events such as data cache hits or misses, branches taken, branched mispredicted, and so forth. Some can interrupt the processor when a certain threshold has been reached. Some can count events in user space and kernel space independently. The pmc interface is intended to allow monitoring from within the kernel as well as monitoring of userland applications. If the hardware can interrupt the CPU in a specific implementation, then it may also be used as a profiling source instead of the clock. IMPLEMENTATION NOTES
All function calls in this interface may be defined as cpp(1) macros. If any function is not implemented as a macro, its prototype must be defined by the port-specific header <machine/pmc.h>. Counters are numbered from 0 to N-1 where N is the number of counters available on the system (see pmc_get_num_counters() below). Upon a process fork, implementations must o Zero performance counters for the new process, and o Inherit any enabled performance counters. DATA TYPES
Each implementation must specify two new types: pmc_evid_t An integer type which can contain the event IDs for a given processor. pmc_ctr_t An integer type defining the value which may be contained in a given counter register. Counters are configured with the struct pmc_counter_cfg. This structure is defined as struct pmc_counter_cfg { pmc_evid_t event_id; pmc_ctr_t reset_value; uint32_t flags; }; flags are currently unused. FUNCTIONS
pmc_get_num_counters(void) Returns the number of counters present on the current system. Valid values for ctr in the interface entry points below are from zero to one less than the return value from this function. pmc_get_counter_type(int ctr) Returns an implementation-dependent type describing the specified counter. If ctr is specified as -1, returns a machine-dependent type describing the CPU or counter configuration. For example, on an ia32 architecture, it may distinguish between 586-, 686-, and K7-style counters. pmc_save_context(struct lwp *l) Saves the PMC context for the current process. This is called just before cpu_switch(9). If there is kernel PMC state, it must be maintained across this call. pmc_restore_context(struct lwp *l) Restores the PMC context for the current process. This is called just after cpu_switch(9) returns. If there is kernel PMC state, it must be maintained across this call. pmc_enable_counter(struct lwp *l, int ctr) Enables counter ctr for the specified process. The counter should have already been configured with a call to pmc_configure_counter(). This starts the counter running if it is not already started and enables any interrupts, as appropriate. pmc_disable_counter(struct lwp *l, int ctr) Disables counter ctr for the specified process. This stops the counter from running, and disables any interrupts, as appropriate. pmc_counter_isrunning(struct lwp *l, int ctr) Returns non-zero if the specified counter in the specified process is running or if the counter is running in the kernel. pmc_counter_isconfigured(struct lwp *l, int ctr) Returns non-zero if the specified counter in the specified process is configured or if the counter is in use by the kernel. pmc_configure_counter(struct lwp *l, int ctr, struct pmc_counter_cfg *cfg) Configures counter ctr according to the configuration information stored in cfg. pmc_get_counter_value(struct lwp *l, int ctr, int flags, uint64_t *pval) Returns the value of counter ctr in the space pointed to by pval. The only recognized flag is PMC_VALUE_FLAGS_CHILDREN which speci- fies that the returned counts should be accumulated values for any exited child processes. pmc_accumulate(struct lwp *l_parent, struct lwp *l_exiting) Accumulates any counter data from the exiting process p_exiting into the counters for the parent process p_parent. pmc_alloc_kernel_counter(int ctr, struct pmc_counter_cfg *cfg) Allocates counter ctr for use by the kernel and configures it with cfg. pmc_free_kernel_counter(int ctr) Returns counter ctr to the available pool of counters that may be used by processes. pmc_start_profiling(int ctr, struct pmc_counter_cfg *cfg) Allocates counter ctr for use by the kernel for profiling and configures it with cfg. pmc_stop_profiling(int ctr) Stops profiling with counter ctr. PMC_ENABLED(struct lwp *l) Returns non-zero if the given process or the kernel is using the PMC at all. SEE ALSO
pmc(1), pmc_control(2), pmc_get_info(2) HISTORY
The pmc interface appeared in NetBSD 2.0. AUTHORS
The pmc interface was designed and implemented by Allen Briggs for Wasabi Systems, Inc. Additional input on the pmc design was provided by Jason R. Thorpe. BSD
May 14, 2010 BSD
All times are GMT -4. The time now is 08:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy