LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2010, 06:16 AM   #1
masavini
Member
 
Registered: Jun 2008
Posts: 285

Rep: Reputation: 6
bash and permutations...


hi,
i have a (quite short) string with n fields:
gigi;gigi2;gigig;gi;ig;

i need a script to get the best fields combination to let the string match a given length value (best match is equal, then equal-1, then equal-2 and so on)...

i.e.:
if length is 7, the output should be:
gigi;gi
gigi;ig

if length is 6, the output should be:
gigi2

it's quite easy to describe, but i can't really figure out how to put it in a script!
can anybody help me? any hint is welcome...
 
Old 07-20-2010, 06:53 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
if length is 7
Length of what ???
 
Old 07-20-2010, 07:23 AM   #3
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
i guess this code is simple enough to be self-explaining...

Code:
CODES="gigi;gigi2;gigig;gi;ig"

permutations.calculator.sh $CODES > permutations.txt

while read LINE
do
LENGTH=${#LINE}) echo $LENGTH";"$LINE >> length.permutations.txt
done < permutations.txt MAXLENGTH=10 touch fitting.permutations.txt while [ "$(head -1 fitting.permutations.txt)" == "" ]; do
if [ $MAXLENGTH -gt 0 ]; then grep '^'$MAXLENGTH';' length.permutations.txt > fitting.permutations.txt let MAXLENGTH=$MAXLENGTH-1 else echo "no permutations with matching length found" > fitting.permutations.txt fi
done
how could permutations.calculation.sh look like?

Last edited by masavini; 07-20-2010 at 07:41 AM.
 
Old 07-20-2010, 07:48 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well I am guessing you want to generate random chunks of your string so here is a link
about generating random numbers.
 
Old 07-20-2010, 09:39 AM   #5
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
almost found a solution!

here is a simple perl script for permutations of an array:

Code:
#!/usr/bin/perl

@fillers = ("a","2","3");

permute([@fillers], []);

sub permute {
my @items = @{ $_[0] };
my @perms = @{ $_[1] };
unless (@items) {
 print "@perms\n";
} else {
 my(@newitems,@newperms,$i);
 foreach $i (0 .. $#items) {
  @newitems = @items;
  @newperms = @perms;
  unshift(@newperms, splice(@newitems, $i, 1));
  permute([@newitems], [@newperms]);
 }
}
}
does anybody how can i pass a bash array to this perl script (@fillers)?
 
Old 07-20-2010, 11:40 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by grail View Post
Length of what ???

Quote:
Originally Posted by grail View Post
Well I am guessing you want to generate random chunks of your string so here is a link
about generating random numbers.
AFAIK he wants to find a combination of the given strings that add up to a certain length.
 
Old 07-20-2010, 11:44 AM   #7
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by MTK358 View Post
AFAIK he wants to find a combination of the given strings that add up to a certain length.
exact!
 
Old 07-20-2010, 10:34 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
Originally Posted by masavini
Quote:
Originally Posted by MTK358
AFAIK he wants to find a combination of the given strings that add up to a certain length.
exact!
I must not be on the same playing field as you guys then, because this now says to me substring.
If that was the case then it would be as simple as:
Code:
if [[ $length -eq 7 ]]
then
    echo ${str:0:6}
fi
Which I would have guessed is not at all what you were after.
 
Old 07-21-2010, 03:33 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
If grail is right it could be even easier as
Code:
echo "${string:0:length - 1}"
or if length is custom and length < actual_string_length then this can be also applied:
Code:
echo "${string:RANDOM % (actual_string_length - length):length - 1}"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create alphanumeric permutations and write to file redscorpion69 Linux - Newbie 9 11-30-2009 06:12 PM
How to create permutations for a wordlist ? frenchn00b Programming 5 08-18-2009 06:03 PM
JavaScript: Working out permutations Andy@DP Programming 2 02-17-2009 11:56 AM
Permutations & recursive Function cdog Programming 3 01-23-2006 08:05 AM
fast algorithm - permutations kev82 Programming 3 08-10-2004 07:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration