PERL : Sort substring occurrences in array of strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL : Sort substring occurrences in array of strings
# 1  
Old 02-03-2011
PERL : Sort substring occurrences in array of strings

Hi,

My developer is on vacation and I am not sure if there is something which is easier for this.

I have an array of strings. Each string in the array has "%" characters in it. I have to get the string(s) which have the least number of "%" in them.

I know how I can get occurrences :
Code:
 
my @str;
...array population here...
my $count = ($str =~ tr/"%"//);

But I am not sure what is the best way to get the string(s) with least number of "%"'s.

Any advise/help is highly appreciated.

Thanks.
sinpeak
# 2  
Old 02-03-2011
Warning: the following is an example of how (positively) twisted Perl is:
Code:
$least = ( sort { my $oa = $a =~ s/(%)/$1/g; my $ob = $b =~ s/(%)/$1/g; $oa <=> $ob } @str )[0];

This will put the string with the least amount of '%' in $least.
# 3  
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

# 4  
Old 02-03-2011
And yet another -

Code:
$
$
$ perl -le '@str = qw( %%abc %ab%c% %a%b%c% ab%c ab%%%%%c ab%%c );
            for (@str) {$x=$_; $count=s/%//g; if (!defined $min or $count<$min){$min=$count; $elem=$x}}
            print "\nElement with the least number of % character => ",$elem'
 
Element with the least number of % character => ab%c
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 02-03-2011
Quote:
Originally Posted by durden_tyler
And yet another -

Code:
$
$
$ perl -le '@str = qw( %%abc %ab%c% %a%b%c% ab%c ab%%%%%c ab%%c );
            for (@str) {$x=$_; $count=s/%//g; if (!defined $min or $count<$min){$min=$count; $elem=$x}}
            print "\nElement with the least number of % character => ",$elem'
 
Element with the least number of % character => ab%c
$
$

Yep,
my post was based on the subject line.
Actually, in this case, the sort could be avoided and your solution is more efficient and appropriate.
# 6  
Old 02-07-2011
Thank to all who responded. Smilie
Appreciate your help.

I have just one issue with the approach given -
As indicated in the original post I need the string(s) (string or strings) having the least number of %'s.

Please advise/suggest on having the list of strings/string which have/has the least number of %'s.
sinpeak
# 7  
Old 02-07-2011
Code:
perl -le'
  @str = qw( 
  ab% %%abc %ab%c% %a%b%c% ab%c ab%%%%%c ab%%c 
    );
  
  do {
    $cnt = tr/%//; push @{$cnt{$cnt}}, $_;
    $min = $cnt unless defined $min and $cnt > $min;
      }
    for @str;  
  print "Element(s) with the least number of % character => ", 
    join ", ", @{$cnt{$min}};
  '

Output:

Code:
% perl -le'
  @str = qw(
  ab% %%abc %ab%c% %a%b%c% ab%c ab%%%%%c ab%%c
    );

  do {
    $cnt = tr/%//; push @{$cnt{$cnt}}, $_;
    $min = $cnt unless defined $min and $cnt > $min;
      }
    for @str;
  print "Element(s) with the least number of % character => ", join ", ", @{$cnt{$min}};
  '
Element(s) with the least number of % character => ab%, ab%c

These 2 Users Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question