Sponsored Content
Full Discussion: Array length: ls and sort
Top Forums UNIX for Beginners Questions & Answers Array length: ls and sort Post 303009450 by genome on Thursday 14th of December 2017 02:06:50 PM
Old 12-14-2017
Array length: ls and sort

Hi there,

I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird.

Code:

    files_info="$(find $input_dir -name "*_CHR$i.info"  | sort )"

    printf ${#files_info[@]}"\n" #print length 

#--loop through array - it works fine
    for x in ${files_info[@]}
    do
        printf "$x\n"
    done

When I loop through the array it works fine. But when I want to print length of array it print 1.
When I debug it with -x, I see '\n'
May be new line character is messing length?

Please guide.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sort on fixed length files

Hi How to sort a fixed length file on a given char range and just display the duplicates. I did search for man sort to find any option but could find any.,something similar to cut -c 1-5,25-35. I have alternate way of doing this by using combination of cut,awk. but this creates extra temp... (6 Replies)
Discussion started by: sach_in
6 Replies

2. Shell Programming and Scripting

Array length in PERL

Hi experts, How to get the length of an Array in PERL. for eg., @Var having 5 elements. regards Anent (5 Replies)
Discussion started by: anent
5 Replies

3. Shell Programming and Scripting

how to sort strings by length?

I'm trying to find the longest word in /usr/share/dict/words The first thing I can think of is to sort the content by length then it would be easy to find out, but then i realize theres no option of sort to sort by length. Could you guys please give me some help?:confused: (7 Replies)
Discussion started by: rockbike
7 Replies

4. Shell Programming and Scripting

Need a sort solution for fixed length file

I have a 1250 byte record that I need to sort in column 10-19 and in column 301. I have tried the sort command, but it looks like it needs delimiters to work. The record can have spaces in a lot of its 1250 columns, but 10-19, and 301 are guaranteed. These columns are numeric too. A sample... (1 Reply)
Discussion started by: mb1201
1 Replies

5. Shell Programming and Scripting

Unix sort for fixed length columns and records

I was trying to use the AIX 6.1 sort command to sort fixed-length data records, sorting by specific columns only. It took some time to figure out how to get it to work, so I wanted to share the solution. The sort man page wasn't much help, because it talks about field delimeters (default space... (1 Reply)
Discussion started by: CheeseHead1
1 Replies

6. Shell Programming and Scripting

sort file specifying record length

I've been searching high and low for this...but, maybe I'm just missing something. I have a file to be sorted that, unfortunately, contains binary data at the end of the line. As you may guess, this binary data may contain a newline character, which messes up the sort. I think I could resolve this... (5 Replies)
Discussion started by: jcagle
5 Replies

7. Shell Programming and Scripting

Array Length Reports as Having Length when it is Empty?

Hello All, I have this script that does stuff like "starting, stopping & restarting" a Daemon Process running on my machine... My main question is why in part of my code (which you will see below) does the Array Length (i.e. ${#PIDS} ) return "1" when I know the Array is empty..? Here is... (17 Replies)
Discussion started by: mrm5102
17 Replies

8. Shell Programming and Scripting

Sort a hash based on the string length of the values

Hi, I want to be able to sort/print a hash based on the string length of the values. For example %hash = ( key1 => 'jeri', key2 => 'corona', key3 => 'una, ); I want to be able to print in the following order (smallest to largest) una,jeri,corona OR... (1 Reply)
Discussion started by: jdilts
1 Replies

9. Shell Programming and Scripting

How to sort when there is variable length decimal points.?

Hi Experts, Quick quesion: I want to sort this in the file , but not working, when using # sort file name 305.932 456.470 456.469 456.468 456.467 172.089 456.467 456.466 456.465 111.573 111.578 111.572 111.572 87.175 87.174 75.898 (4 Replies)
Discussion started by: rveri
4 Replies

10. Programming

Integer array length

Hello; When I wrote a function to print out an array, void p_array(int arr) { int i; int size = sizeof(arr) / sizeof(int); // int size = sizeof (arr) / sizeof (arr); for (i = 0; i < size; i++) printf("%d ", arr); printf("\n"); }I could only print out the... (19 Replies)
Discussion started by: yifangt
19 Replies
NATSORT(3)								 1								NATSORT(3)

natsort - Sort an array using a ";natural order" algorithm

SYNOPSIS
bool natsort (array &$array) DESCRIPTION
This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in sort(3)) can be seen in the example below. PARAMETERS
o $array - The input array. RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ |5.2.10 | | | | | | | Zero padded numeric strings (e.g., '00005') now | | | essentially ignore the 0 padding. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 natsort(3) examples demonstrating basic usage <?php $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png"); asort($array1); echo "Standard sorting "; print_r($array1); natsort($array2); echo " Natural order sorting "; print_r($array2); ?> The above example will output: Standard sorting Array ( [3] => img1.png [1] => img10.png [0] => img12.png [2] => img2.png ) Natural order sorting Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png ) For more information see: Martin Pool's Natural Order String Comparison page. Example #2 natsort(3) examples demonstrating potential gotchas <?php echo "Negative numbers "; $negative = array('-5','3','-2','0','-1000','9','1'); print_r($negative); natsort($negative); print_r($negative); echo "Zero padding "; $zeros = array('09', '8', '10', '009', '011', '0'); print_r($zeros); natsort($zeros); print_r($zeros); ?> The above example will output: Negative numbers Array ( [0] => -5 [1] => 3 [2] => -2 [3] => 0 [4] => -1000 [5] => 9 [6] => 1 ) Array ( [2] => -2 [0] => -5 [4] => -1000 [3] => 0 [6] => 1 [1] => 3 [5] => 9 ) Zero padding Array ( [0] => 09 [1] => 8 [2] => 10 [3] => 009 [4] => 011 [5] => 0 ) Array ( [5] => 0 [1] => 8 [3] => 009 [0] => 09 [2] => 10 [4] => 011 ) SEE ALSO
natcasesort(3), The comparison of array sorting functions, strnatcmp(3), strnatcasecmp(3). PHP Documentation Group NATSORT(3)
All times are GMT -4. The time now is 07:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy