Sponsored Content
Top Forums UNIX for Dummies Questions & Answers awk: syntax for "if (array doesn't contain a particular index)" Post 302703631 by beca123456 on Thursday 20th of September 2012 05:45:30 AM
Old 09-20-2012
Thanks elixir_sinari for this explanation !

However, I am a bit confused with "values" and "parameters".
if I take the example of the code I' ve written in the previous post (same as at the end here).
"w" is the parameter defining the value "$1".

Quote:
So, it's always better to pass data to functions using the parameters so that you are working on a copy of the data and in no way, changing/destroying with the original data.
How do you pass data to functions using parameters?
Do you need to assign the parameter ("w") to the value ("$1") before invoking the function? Tell me if I am wrong, but with the code below it would give:
Code:
...
{function}

{ for(i=1; i<=2; i++)
   	  $1 = w
          print xlate(w, i)        # and not print xlate($1, i)
}

{I can keep using $1 after}

Quote:
(except for those passed as parameters while invoking the function)
That's what I did before, but $1 is not recognised once I invoked the function. ???

Code I am referring to:
Code:
BEGIN {
        xtab["11"]="a";
        xtab["12"]="b";
        xtab["13"]="c";
    }
  
    # accepts word and starting offset 
    function xlate( w, start)
    {
        xw = ""
        n = substr(w, start, 2)    # seed n with first pair 
        while( n != "" )
        {
            xw = xw (n in xtab ? xtab[n] : "X")
            start = start + 2;
            n = substr(w, start, 2)    # look ahead to next; exit loop if at end
        }
        return xw
    }

    { for(i=1; i<=2; i++)
   	  print xlate($1, i)
    }

    { for(i=1; i<=2; i++)
    		print $1 = xlate($1, i)
                if($1 !~ /X/){
            	    print $1
            	    }
     }

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk Syntax Error "!~"

This is my Awk command. direct_count=`awk -F; 'BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ } END { print direct_count }' ls.txt` I get a syntax error. BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ }... (6 Replies)
Discussion started by: yongho
6 Replies

2. 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

3. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies

4. Shell Programming and Scripting

awk array starting with "ord"

I was writing a awk function and had a error I was wondering about. It revolves around a Multidimensional array starting with ord example: if ( _e == 5 ) { lmrb=$5 ; lmtb=$6 ; larb=$7 ; latb=$8 } ... (2 Replies)
Discussion started by: timj123
2 Replies

5. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

6. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

7. Programming

C: Initialize "const" array from the "heap"

Hello, I am working on solving an NP-Complete problem, so it is very important that operations and data with limited integer-argument ranges be computed using immutable look-up-tables contained entirely in CPU cache. Retrieval of the look-up-table data must never leave the CPU once initially... (6 Replies)
Discussion started by: HeavyJ
6 Replies

8. 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

9. HP-UX

DLT 7000 tape drive failing with "write error on a record in the index"

HP rp5450 (L2000) running HP-UX 11.11B Using DLT 7000 and DLT 4000 tape drives for nightly full backups Backup jobs created by SAM DLT 7000 cron entry is as follows: 00 2 * * 1-6 /usr/sam/lbin/br_backup DLT FULL Y /dev/rmt/0m /var/sam/graphLCAa17036 root Y 1 N > /var/sam/SAM_br_msgs 2>&1... (1 Reply)
Discussion started by: dreh99
1 Replies

10. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies
ARRAY(3)								 1								  ARRAY(3)

array - Create an array

SYNOPSIS
array array ([mixed $...]) DESCRIPTION
Creates an array. Read the section on the array type for more information on what an array is. PARAMETERS
o $... - Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the big- gest integer index + 1. Note that when two identical index are defined, the last overwrite the first. Having a trailing comma after the last defined array entry, while unusual, is a valid syntax. RETURN VALUES
Returns an array of the parameters. The parameters can be given an index with the => operator. Read the section on the array type for more information on what an array is. EXAMPLES
The following example demonstrates how to create a two-dimensional array, how to specify keys for associative arrays, and how to skip-and- continue numeric indices in normal arrays. Example #1 array(3) example <?php $fruits = array ( "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third") ); ?> Example #2 Automatic index with array(3) <?php $array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13); print_r($array); ?> The above example will output: Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 13 [4] => 1 [8] => 1 [9] => 19 ) Note that index '3' is defined twice, and keep its final value of 13. Index 4 is defined after index 8, and next generated index (value 19) is 9, since biggest index was 8. This example creates a 1-based array. Example #3 1-based index with array(3) <?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?> The above example will output: Array ( [1] => January [2] => February [3] => March ) As in Perl, you can access a value from the array inside double quotes. However, with PHP you'll need to enclose your array between curly braces. Example #4 Accessing an array inside double quotes <?php $foo = array('bar' => 'baz'); echo "Hello {$foo['bar']}!"; // Hello baz! ?> NOTES
Note array(3) is a language construct used to represent literal arrays, and not a regular function. SEE ALSO
array_pad(3), list(3), count(3), range(3), foreach, The array type. PHP Documentation Group ARRAY(3)
All times are GMT -4. The time now is 04:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy