Sponsored Content
Top Forums Shell Programming and Scripting PERL : Sort substring occurrences in array of strings Post 302493676 by radoulov on Thursday 3rd of February 2011 10:41:18 AM
Old 02-03-2011
Code:
perl -le'

@a = qw(
  a%b%
  ab%%%a%b
  b%%%ab
  );
  
print join $/, 
  map $_->[1], 
    sort { 
      $a->[0] <=> $b->[0] 
      } map [ tr/%//, $_ ], 
        @a;
  '

Output:

Code:
% perl -le'

@a = qw(
  a%b%
  ab%%%a%b
  b%%%ab
  );

print join $/,
  map $_->[1],
    sort {
  $a->[0] <=> $b->[0]
  } map [ tr/%//, $_ ],
    @a;
  '  
a%b%
b%%%ab
ab%%%a%b

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sort() array of strings in perl

I have a perl script with an array of clients. @arr = ("The ABC Corp", "AA Corp.", "BB Corp"); If I run @a = sort (@arr); I will get @a = ("AA Corp", "BB Corp", "The ABC Corp"); but I want @a = ("AA Corp, "The ABC Corp", "BB Corp"); How do I sort array without changing... (2 Replies)
Discussion started by: photon
2 Replies

2. Shell Programming and Scripting

How to sort lines by substring

Dear all There is a file which contains the following formatted files. I need to sort it by substring(strings after dot) in order to process efficiently. Please give me any idea how to sort it. Sample file: 1AAABBBCCC.20080401 1AAABBBCCC.20080402 2AAABBBCCC.20080401... (3 Replies)
Discussion started by: mr_bold
3 Replies

3. Shell Programming and Scripting

Can we pass an array of strings from a Perl Program to a Shell Script?

Hi Folks, The subject is my question: Can we pass an array of strings from a Perl Program to a Shell Script? Please provide some sample code. Thanks ---------- Post updated at 11:52 PM ---------- Previous update was at 11:43 PM ---------- I got it. Its here:... (0 Replies)
Discussion started by: som.nitk
0 Replies

4. UNIX for Dummies Questions & Answers

Sort after 2. column in array in Perl

Hey How do I sort an array numerically after the second column? My values are integers like 1, 2, 3, 4... and they are not unique, so I can't just reverse my hash and sort by keys. I wanna sort my file/array so that I get the lines with the highest value in the top - that is descending. ... (2 Replies)
Discussion started by: Banni
2 Replies

5. Shell Programming and Scripting

perl sort array by field

Hi Everyone, Any simple code can simplify the code below, please advice. Thanks # cat 2.pl #!/usr/bin/perl use warnings; use strict; my @aaaaa = <DATA>; my @uids; foreach (@aaaaa) { my @ccccc = split (",", $_); push @uids, $ccccc;... (3 Replies)
Discussion started by: jimmy_y
3 Replies

6. UNIX for Dummies Questions & Answers

Print number of occurrences of many different strings

People, I need your help with making a script which will 1. take as an input the number of lines, smth like this: ((RUBROBACTER_1_PE1288 (((SALINISPORA_1_PE1863 SALINISPORA_1_PE1828)100 ((NOCARDIOIDES_2_PE2419 PROPIONIBACTERIUM_1_PE1395)96 ((((((((CORYNEBACTERIUM_1_PE1119... (3 Replies)
Discussion started by: roussine
3 Replies

7. Shell Programming and Scripting

how many occurrences of different strings are there in each FILE.

Hello , I need some help to pull the data from different files, simultaneously for the string provided. I want to search below strings. PTN:3763427632478 IDB:3298734287438 PTN:8734983298738 From the files BELOW CODE_FILE_LOG1 CODE_FILE_LOG2 CODE_FILE_LOG3 CODE_FILE_LOG4 (3 Replies)
Discussion started by: baraghun
3 Replies

8. UNIX for Dummies Questions & Answers

Replace all occurrences of strings with parentheses

Hi, I tried to adapt bartus's solution to my problem, without success. I want to replace all the occurences of this: with: , where something can contain an arbitrary number of balanced parens and brakets. Any ideas ? Best, (1 Reply)
Discussion started by: ff1969ff1969
1 Replies

9. Linux

How to sort the number of occurrences

file:///C:/Users/TSHEPI%7E1.LEB/AppData/Local/Temp/moz-screenshot.pngATM@ubuntu:~$ cat numbers2 | sort -n | uniq -c 1 7 1 11 2 10 3 the 1st numbers are the counts from the command "uniq -c", which represent the number of occurrences of each in the file. The "sort -n"... (4 Replies)
Discussion started by: lebogot
4 Replies

10. Shell Programming and Scripting

GREP between last occurrences of two strings

Hi I have the server.log file as: Server Stopped ABC DEF GHI JKL Server Started MNO PQR STU Server Stopped VWX YZ ABC Server Started Server Stopped 123 456 789 (9 Replies)
Discussion started by: ankur328
9 Replies
IMPLODE(3)								 1								IMPLODE(3)

implode - Join array elements with a string

SYNOPSIS
string implode (string $glue, array $pieces) DESCRIPTION
string implode (array $pieces) Join array elements with a $glue string. Note implode(3) can, for historical reasons, accept its parameters in either order. For consistency with explode(3), however, it may be less confusing to use the documented order of arguments. PARAMETERS
o $glue - Defaults to an empty string. o $pieces - The array of strings to implode. RETURN VALUES
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each ele- ment. EXAMPLES
Example #1 implode(3) example <?php $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone // Empty string when using an empty array: var_dump(implode('hello', array())); // string(0) "" ?> NOTES
Note This function is binary-safe. SEE ALSO
explode(3), preg_split(3), http_build_query(3). PHP Documentation Group IMPLODE(3)
All times are GMT -4. The time now is 12:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy