Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Break sentence into two part

by Mjpaddy (Acolyte)
on Jan 24, 2015 at 06:36 UTC ( [id://1114336]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, Is there is any module or method which can make a sentence into two equal part or half or partially half

Suppose this is a sentence as follow:

Perl 5 is a highly capable, feature-rich programming language with over 27 years of development.

which method will be used in order to get result like:

$firsthalf: Perl 5 is a highly capable, feature-rich programming

$sechalf : language with over 27 years of development.

Thanks in advanced

Replies are listed 'Best First'.
Re: Break sentence into two part
by rnewsham (Curate) on Jan 24, 2015 at 07:30 UTC

    Something like below may do what you want. Although it's not exactly clear from your question why you want to do this, or what you have tried so far. You may get a better answer, if you can provide more details of what problem you are trying to solve and example code you have tried.

    use strict; use warnings; my $sentence = 'Perl 5 is a highly capable, feature-rich programming l +anguage with over 27 years of development.'; my @words = split( ' ', $sentence); my $midpoint = sprintf("%d", scalar @words / 2); my ( @first_half, @second_half ); my $count = 0; for ( @words ) { push @first_half, $_ if $count <= $midpoint; push @second_half, $_ if $count > $midpoint; $count++; } print join( ' ', @first_half ) . "\n"; print join( ' ', @second_half ) . "\n";
    <code> Output: Perl 5 is a highly capable, feature-rich programming language with over 27 years of development

      Yes, that’s how I would approach the problem. But there’s no need to populate new arrays using push, etc.: just use array slices directly:

      my $first_half = join(' ', @words[0 .. $midpoint]); my $second_half = join(' ', @words[$midpoint + 1 .. $#words ]);

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The midpoint calculation also can be written as:
      my $midpoint = int(@words / 2);
Re: Break sentence into two part
by toolic (Bishop) on Jan 24, 2015 at 14:06 UTC
    If you just need to break lines for output, Text::Wrap can help:
    use warnings; use strict; use Text::Wrap qw(wrap); $Text::Wrap::columns = 55; my $text = 'Perl 5 is a highly capable, feature-rich programming langu +age with over 27 years of development.'; print wrap('', '', $text), "\n"; __END__ Perl 5 is a highly capable, feature-rich programming language with over 27 years of development.
Re: Break sentence into two part
by karlgoethebier (Abbot) on Jan 24, 2015 at 19:38 UTC

    Code reuse ;-)

    #!/usr/bin/env perl use strict; use warnings; use List::MoreUtils qw(natatime); use feature qw (say); my @sentence = split / /, qq(Perl 5 is a highly capable, feature-rich programming language with +over 27 years of development); my $iterator = natatime scalar( @sentence / 2 ) + 1, @sentence; while ( my @half = $iterator->() ) { say join " ", @half; } __END__ karls-mac-mini:monks karl$ ./1114336.pl Perl 5 is a highly capable, feature-rich programming language with over 27 years of development

    See also Re: writing array as pairwise and List::MoreUtils.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Break sentence into two part
by roboticus (Chancellor) on Jan 24, 2015 at 16:59 UTC

    Mjpaddy:

    You've already received some nice answers, so you won't want to use this ridiculous one. I just created it to amuse myself. I'm only posting it because: (a) It amused me to do so, and (b) 'cause I appropriated 2teez's signature line as part of the example, who might also be amused... ;^D

    sub splitit { my ($left, @words, $right, $doit) = split /\s+/, shift; $right = pop @words; $doit = sub { my ($left, $right, @rest) = @_; if (@rest) { if (length($left) < length($right)) { return $doit->($left . ' ' . shift @rest, $right, @res +t); } else { return $doit->($left, pop(@rest) . ' ' . $right, @rest +); } } return $left, $right; }; $doit->( $left, $right, @words); } while (<DATA>) { s/\s+$//; print join(" | ", splitit($_)), "\n" unless /^$/;; } __DATA__ Now is the time for all good men to come to the aid of their party. The quick red fox jumped over the lazy brown dog. Time flies like an arrow, fruit flies like a banana. If you tell me, I'll forget. If you show me, I'll remember. If you involve me, I'm calling the cops.

    Note: This code is an example of *bad* programming style, so don't use it, and it uses recursion instead of a simple loop which would be simpler and more efficient.

    Update: A better version, which is still useless:

    #!/usr/bin/env perl # # split a sentence into two roughly equal parts # use strict; use warnings; sub splitit { my $sentence = shift; my @words = split /(\s+)/, $sentence; my $doit; $doit = sub { my ($left, $right, @rest) = @_; if (@rest) { if (length($left) < length($right)) { return $doit->($left . shift @rest, $right, @rest); } else { return $doit->($left, pop(@rest) . $right, @rest); } } return $left, $right; }; return $doit->( '', '', @words); }

    Finally, using a loop:

    sub splitit { my @words = split /(\s+)/, shift; my ($left, $right) = ('', ''); while (@words) { if (length($left) < length($right)) { $left .= shift @words; } else { $right = pop(@words) . $right; } } return $left, $right; }

    ...roboticus

    If ya wanna get to be good at anything, keep doing it. If ya wanna keep doing it, play with it and make it fun.

Re: Break sentence into two part
by 2teez (Vicar) on Jan 24, 2015 at 12:42 UTC

    Hi Mjpaddy,
    ..which can make a sentence into two equal part or half or partially half

    Others have answered you well, but what got my attention was the two equal part or half "wordings", in your question. Though I might be wrong, in the sense that the OP's example might have shown what s/he might want.

    But that being said, IMHO, one can get the sentence into two equals, considering all the character in the sentence space in inclusive, like so:

    use warnings; use strict; my $wd = 'Perl 5 is a highly capable, feature-rich programming language with ov +er 27 years of development.'; my $midpt = ( split //, $wd ) / 2 ; print join( $/ => ( "First-Half:", substr( $wd, 0, $midpt ), "Second-Half:", substr( $wd, $midpt ) ) ), $/;
    Output:
    First-Half: Perl 5 is a highly capable, feature-rich program Second-Half: ming language with over 27 years of development.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Using the length function might be slightly simpler than splitting the sentence into a list of individual characters.

      Je suis Charlie.

        +1 Laurent_R,
        Oh! yes. that is truth in this case, only that I wasn't looking in that direction. :)

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Break sentence into two part
by AnomalousMonk (Archbishop) on Jan 24, 2015 at 23:02 UTC

    More useless code:

    c:\@Work\Perl>perl -wMstrict -le "my $sent = 'Perl 5 is a highly capable, feature-rich programming language with + over 27 years of development'; ;; my $len = int(length($sent) / 2); ;; my ($first, $second) = $sent =~ m{ (\A .{$len,}?) \s+ (.*) }xms; ;; print qq{'$first'}; print qq{'$second'}; " 'Perl 5 is a highly capable, feature-rich programming' 'language with over 27 years of development'


    Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1114336]
Approved by rnewsham
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-19 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found