http://www.perlmonks.org?node_id=1006387

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks I have 2 strings in this way:
String1: "big business"
String2: "big movie companies"
What I want to generate is something like "big business and movie companies" which is the smart concatenation. If both strings has the same size and if the position of the common word is the same the problem is easier. There are issues when the position of the common token is not same. Any idea would be appreciated.
The example is very dummy but when the length and the positions are different, then the normal positioning cant be used.
for $i (0 .. $#p2) { #print "$p1[$i] vs $p2[$i]\n"; $mism_pos{$i} = 1 if ($p1[$i] +eq $p2[$i]); } #print sort keys %mism_pos,"\n"; if (keys %mism_pos) { for $i (0..$#p2) { if (defined $mism_pos{$i}) + { if (@new1 && @new2) { push @new,@new1,'a +nd',@new2,$p2[$i]; @new1=(); @new2=(); } else { push @new,$p2[$i]; } } else { push @new1,$p1[$i]; push @new2,$p2[$i]; } } }
This solution would work if the length and positions are the same. However, I get also this error: "Use of uninitialized value within @p1 in concatenation (.) or string" and I get this as answer: "big"

Replies are listed 'Best First'.
Re: Concatenating strings with different length
by GrandFather (Saint) on Nov 30, 2012 at 04:31 UTC
Re: Concatenating strings with different length
by Kenosis (Priest) on Nov 30, 2012 at 03:23 UTC

    What about something like the following?

    use strict; use warnings; my %fragments; while ( my $line = <DATA> ) { chomp $line; my ( $term, $rest ) = split ' ', $line, 2; push @{ $fragments{$term} }, $rest; } for my $term ( keys %fragments ) { print "There are $term " . ( join ' and ', @{ $fragments{$term} } +) . ".\n"; } __DATA__ big businesses fast red cars big movie companies fast light planes fast animals big bad wolves

    Output:

    There are big businesses and movie companies and bad wolves. There are fast light planes and red cars and animals.
      Or even:
      #!/usr/bin/perl use warnings; use strict; #use Data::Dumper; my %fragments; while ( my $line = <DATA> ) { chomp $line; my @words = split / /, $line; for my $i (0 .. $#words - 1) { push @{ $fragments{$words[$i]} }, join ' ', @words[0 .. $i-1, $i+1 .. $#words]; } } for my $term ( keys %fragments ) { print "There are $term " . ( join ' and ', @{ $fragments{$term} } +) . ".\n"; } __DATA__ big businesses fast red cars big smart companies fast light planes fast animals big bad wolves small bad girls bad growling bears
      Output:
      There are small bad girls. There are light fast planes. There are growling bad bears. There are bad big wolves and small girls and growling bears. There are big businesses and smart companies and bad wolves. There are smart big companies. There are red fast cars. There are fast red cars and light planes and animals.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Excellent, choroba!

        Building on your and GrandFather's suggestions, there's the following:

        #!/usr/bin/perl use warnings; use strict; use Lingua::EN::Tagger; my %fragments; my $p = new Lingua::EN::Tagger; my @adjectives = qw/nervous nutty obnoxious outrageous panicky repulsive scary selfish sore tense terrible testy thoughtless tired troubled upset uptight weary wicked worried/; my @stems = ( "It's been said that there were many", 'Goldfish spoke with', 'There were three', 'People used to see 42', 'There were quietly-roaming groups of' ); while ( my $line = <DATA> ) { chomp $line; my $adjective = $adjectives[ int rand $#adjectives + 1 ]; $line = $p->add_tags($line) =~ s/<nns>/$adjective /gr =~ s/<.+?>// +gr; my @words = split / /, $line; for my $i ( 0 .. $#words - 1 ) { push @{ $fragments{ $words[$i] } }, join ' ', @words[ 0 .. $i - 1, $i + 1 .. $#words ]; } } print "Once upon a time...\n\n"; my @sentences; for my $term ( keys %fragments ) { my $stem = $stems[ int rand $#stems + 1 ]; push @sentences, "$stem $term " . ( join ' and ', @{ $fragments{$term} } ) . '.'; } print join ' ', @sentences; print "\n\nThe times have certainly changed.\n\nThe end."; __DATA__ big businesses fast red cars big smart companies fast light planes fast animals big bad wolves small bad girls bad growling bears

        Sample output:

        Once upon a time...

        People used to see 42 small bad outrageous girls. There were three outrageous fast red cars and small bad girls. Goldfish spoke with light fast scary planes. There were three growling bad nervous bears. There were quietly-roaming groups of nervous big bad wolves and bad growling bears. There were three scary fast light planes. There were quietly-roaming groups of tense fast animals. Goldfish spoke with bad big nervous wolves and small outrageous girls and growling nervous bears. People used to see 42 big wicked businesses and smart selfish companies and bad nervous wolves. Goldfish spoke with smart big selfish companies. Goldfish spoke with red fast outrageous cars. Goldfish spoke with wicked big businesses. It's been said that there were many fast red outrageous cars and light scary planes and tense animals. There were quietly-roaming groups of selfish big smart companies.

        The times have certainly changed.

        The end.

        An emerging Perl storyteller...

        Well this works if you know the common word is the first word. IF not, then this never works. In the cases such as "modern game" and "classic game" it will not work.
Re: Concatenating strings with different length
by karlgoethebier (Abbot) on Nov 30, 2012 at 15:26 UTC

    Consider using perltidy for formatting your code.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Concatenating strings with different length
by Anonymous Monk on Nov 30, 2012 at 02:46 UTC
    Homework or spam?
      None! Why should it be spam?

        I've seen this type of question before, and its always been for homework or spam generation -- you say it isn't, great, whats it for?