Sponsored Content
Top Forums Shell Programming and Scripting Select / Print odd-even elements of an array Post 302979931 by Don Cragun on Monday 22nd of August 2016 02:37:05 AM
Old 08-22-2016
Hi H squared,
That isn't the way arrays work. Reading an array creates elements based on field splitting; not arrays of individual characters. But, both recent versions of bash and 1993 or later versions of ksh support extensions to the standard providing simplified for loops and substring variable expansions that do what is needed here:
Code:
#!/bin/bash
{	read -r n
	printf 'Expecting to process %s lines...\n' "$n"
	while IFS= read -r text
	do	printf 'Processing text: "%s"\n' "$text"
		for((i = 0; i < ${#text}; i += 2))
		do	printf '%s' "${text:$i:1}"
		done
		printf ' '
		for((i = 1; i < ${#text}; i += 2))
		do	printf '%s' "${text:$i:1}"
		done
		echo
	done
} < file

which with the input you showed us in a file named file produces the output:
Code:
Expecting to process 2 lines..
Processing text: "Haider"
Hie adr
Processing text: "Bash"
Bs ah

This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Deleting Array Elements

Hi, I am writing a BASH shell script. I have an array that will contain IN ANY ORDER the following elements: DAY 8D MO NS. I would like to erase the element DAY, but since the order of the elements in the array are random, I will not know which element # DAY is (ie it's not as simple as... (3 Replies)
Discussion started by: msb65
3 Replies

2. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

3. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

4. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

5. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

6. Shell Programming and Scripting

Grouping array elements - possible?

I have a script which takes backup of some configuration files on my server. It does that by using an array which contains the complete path to the files to backup. It copys the files to a pre defined dir. Each "program" has it's own folder, ex. apache.conf is being copied to /predefined... (7 Replies)
Discussion started by: dnn
7 Replies

7. Shell Programming and Scripting

Multiplication of array elements

Hi, I can't find out how to create correct code to get multiplication of each elements of array. Let's say I enter array into command line (2 3 4 5 6 8) and i need output 2*3*4*5*6*8=5760. I tried this one, but answer is 0. for i in $@; do mult=$((mult*i))done echo "mult: " $mult ... (4 Replies)
Discussion started by: rimasbimas
4 Replies

8. UNIX for Dummies Questions & Answers

How to declare an array in UNIX and print the elements with tab delimits?

Hello, In a shell script, I want to declare an array and subsequently print the elements with tab delimits. My array has the following structure and arbitrary elements: myArray=('fgh' 'ijk' 'xyz' 'abc'); Next, I would like to print it with a '\n' at the end. Thanks for your input! ... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

9. Shell Programming and Scripting

Random elements from array

Hi I wanted to print random elements from an array at bash shell I use the following code, but I always see first element getting printed #!/bin/bash c=1 expressions=(pink red white yellow purple) while ]; do echo "The value of RANDOM is $RANDOM" selectedexpression=${expressions}]};... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

10. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies
Text::Flow(3pm) 					User Contributed Perl Documentation					   Text::Flow(3pm)

NAME
Text::Flow - Flexible text flowing and word wrapping for not just ASCII output. SYNOPSIS
use Text::Flow; # use it on ASCII text ... my $flow = Text::Flow->new( check_height => sub { my $paras = shift; sum(map { scalar @$_ } @$paras) <= 10; }, wrapper => Text::Flow::Wrap->new( check_width => sub { length($_[0]) < 70 } ), ); my @sections = $flow->flow($text); # @sections will now be an array of strings, each string # will contain no more than 10 lines of text each of which # will be no longer then 70 characters long # or use it on text in a PDF file ... my $flow = Text::Flow->new( check_height => sub { my $paras = shift; (sum(map { scalar @$_ } @$paras) * $pdf->get_font_height) < 200; }, wrapper => Text::Flow::Wrap->new( check_width => sub { my $string = shift; $pdf->get_string_width($string) < 100 }, ) ); my @sections = $flow->flow($text); # @sections will now be an array of strings, each string # will contain text which will fit within 200 pts and # each line will be no longer then 100 pts wide DESCRIPTION
This module provides a flexible way to wrap and flow text for both ASCII and non-ASCII outputs. Another Text Wrap module!!?!?! The main purpose of this module is to provide text wrapping and flowing features without being tied down to ASCII based output and fixed- width fonts. My needs were for a more sophisticated text control in PDF and GIF output formats in particular. METHODS
new (%params) This constructs the new Text::Flow instance, and accepts parameters for the "wrapper" and "check_height" variables. wrapper (?$wrapper) This is the accessor for the internally help Text::Flow::Wrapper instance which is used by Text::Flow to wrap the individual lines. check_height This is the accessor for the CODE ref which is used to check the height of the current paragraph. It gets as an argument, an array-ref of paragraphs, each of which is also an array-ref of text lines. The most common usage is shown in the SYNOPSIS above, but you can really do anything you want with them. It is expected to return a Boolean value, true if the height is still okay, and false if the max height has been reached. flow ($text) This method preforms the text flowing. It returns an array of strings which can be treated as complete blocks of text. It will handle paragraph breaks and line breaks for you. Introspection meta Returns the Moose meta object associated with this class. TODO
I wanted to write some tests for using this with GD modules as well. I suppose we will have to wait until 0.02 for that. SIMILAR MODULES
There are a bunch of other text wrapping and flowing modules out there, but they are all meant for use in ASCII outout only. This just didn't work for my needs, so I wrote this one. If you need to wrap ASCII text and want to do it in a much simpler manner then this module provides, then I suggest checking out those modules. This module is specifically made for when those other modules are no longer powerful and flexible enough. BUGS
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. AUTHOR
Stevan Little <stevan@iinteractive.com> COPYRIGHT AND LICENSE
Copyright 2007 by Infinity Interactive, Inc. <http://www.iinteractive.com> This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2007-05-21 Text::Flow(3pm)
All times are GMT -4. The time now is 02:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy