473,706 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(patch for Bash) list comprehension and filtering

1. Here is shell version of Python filter() for array. Essentially,
you apply a command on each array element, and extract only those
elements which it returns success (0). This is specialized form of
list comprehension where you can contruct an array from another
array, using some sort of test or filtering.

Usage is
arrayfilter [-a var] command array
where 'command' is a function, shell script, or external executable,
which takes one argument (ie. the array elements). If 'command'
return success (0), then print the element; and, if it fails
(non-zero), then skip the element. If -a option is given, then the
output will be save into array variable 'var', instead of default
stdout.

For example,
a=(1 a 2 b 3 c)
func () { [[ $1 == [a-z] ]]; }

arrayfilter func a --> a b c
arrayfilter -a b func a --> b[1]=a b[3]=b b[5]=c
2. More generally, here is shell version of Python list comprehension.
Idea is to apply some test or command on each array element, and
collect the outputs as parameter expansion. So, for each element of
array variable 'var',
${var| command }
will expand to the output of 'command' which takes one argument. If
'command' returns NULL, then that element is skipped. Usage is very
similar to other parameter expansion, and both '*' and '@' work as
expected.

For example,
a=(1 a 2 b 3 c)
func () { [[ $1 == [a-z] ]] && echo ".$1."; }

func x --> .x.
${a[*]| func } --> .a. .b. .d.
Ref:
http://freshmeat.net/projects/bashdiff/
http://home.eol.ca/~parkw/index.html#bash
help arrayfilter
help '${var|'

--
William Park, Open Geometry Consulting, <op**********@y ahoo.ca>
Toronto, Ontario, Canada
Jul 18 '05 #1
0 1721

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2331
by: Jeff Epler | last post by:
Hello. Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild Python from source, because it also requires a change to Grammar. 1. Edit Python-2.3/Grammar/Grammar and add an alternative to the "listmaker" production: -listmaker: test ( list_for | (',' test)* )
35
2972
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you could do: result = for element in list: if element == 'blah':
6
3382
by: William Park | last post by:
(crossposted to comp.lang.python, because this may be of interest to them.) Python has try-block, within which you can raise exception. Once it's raised, execution breaks out of the try-block and is caught at the end of try-block. Now, Bash has similiar feature. I've added try-block and 'raise' builtin into Bash-3.0. Typical usage would go something like try
7
1396
by: Eelco Hoekema | last post by:
I'm trying to get a list of tuples, with each tuple consisting of a directory, and a list of files. I only want a tuple if and only if the filtered list of files is not empty. And, i want the list of files in the tuples to be filtered. For this, i came up with the following code: <code> # song filter: will return true if the file seems to be an mp3 file. # (may not be the best way to do this) def song(f):
23
4042
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly appreciated. x = 0 for each in _dbases: if each <> ".dbf": del each # also tried: del _dbases x = x + 1 I must be doing something wrong, but it acts as though it is....
1
1390
by: Stelios Xanthakis | last post by:
Hi all. Basically, I'd like to use python as my system shell. It's not suitable for a general purpose shell but since it is very programmable it can eventually do whatever bash does and even more. One thing I'd like python to do is: attach the *python code* of a function to a function object. The same for class. This can be done in python as it is but it doesn't feel very right. I
18
460
by: a | last post by:
can someone tell me how to use them thanks
4
1812
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a lc expression as a for loop to drive the recursion. Thanks for any insight! Gregory
0
1983
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 385 open (+21) / 3790 closed (+21) / 4175 total (+42) Bugs : 1029 open (+43) / 6744 closed (+43) / 7773 total (+86) RFE : 262 open ( +4) / 291 closed ( +4) / 553 total ( +8) New / Reopened Patches ______________________
0
8781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9286
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9055
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7911
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2502
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2095
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.