The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
PHP arrays as array elements JerryHone Shell Programming and Scripting 3 12-29-2008 05:13 AM
Accessing array elements mansa Shell Programming and Scripting 1 10-30-2008 06:28 AM
Deleting Array Elements msb65 UNIX for Dummies Questions & Answers 3 10-21-2008 02:29 PM
Rearrange array elements-Pls help tj23 Shell Programming and Scripting 4 06-20-2008 09:29 AM
To return the elements of array Sudhakar333 Shell Programming and Scripting 5 08-06-2007 02:20 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-02-2009
shellwell shellwell is offline
Registered User
  
 

Join Date: May 2009
Posts: 15
awk - array elements as condition

Hi,

can I use array elements ( all ) in conditional statements?
the problem is ,the total number of elements is not known.

e.g

A is an array with elements - 1,2,3

now if i want to test if the 1 st field of input record is either 1,2 or 3, i can do something like this

Code:
if ( $1 ~ /^A[1]$|^A[2]$|^A[3]$/) { print  YES }
but i want something like..

Code:
if ( $1 ~ /^A[1]$|^A[2]$|^A[3]$/| logical OR till array's last element) { print  YES }

Is that possibe?
  #2 (permalink)  
Old 07-02-2009
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 968
When you say:
Code:
$1 ~ /^A[1]$/
That would be equivalent (if it worked) to:
Code:
$1 == A[1]
You can't use / ... / to do what you are trying to do.

Have a look at:
The GNU Awk User's Guide
(7.5 Scanning All Elements of an Array)
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0