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

Re: A Little String Help Please

by c4onastick (Friar)
on May 05, 2007 at 17:45 UTC ( #613752=note: print w/ replies, xml ) Need Help??


in reply to A Little String Help Please

This is an interesting problem. I just finished reading (well started reading) Effecive Perl Programming and this is actually a good place to use an array slice (in my attempt at a solution). This is by no means a final solution, but hopefully it'll get you close and generate some ideas from other (wiser) monks.

#!/usr/bin/perl use warnings; use strict; my $n = shift; $n--; my $test = 'A huge string separated by lots and lots of words that I\' +d like to break up into n shorter strings of length y'; my @words = split /\s+/, $test; #First method, doesn't pickup the leftovers for(my $i = 0; $i+$n < $#words; $i += $n+1) { print join(' ', @words[$i..$i+$n]), "\n"; } print "\n\n"; #second method, picks up the leftovers my $j = 0; while($j+$n < $#words) { print join(' ', @words[$j..$j+$n]), "\n"; $j += $n+1; } print join(' ', @words[$j..$#words]), "\n";


Comment on Re: A Little String Help Please
Download Code
Re^2: A Little String Help Please
by GrandFather (Cardinal) on May 05, 2007 at 20:02 UTC

    splice is the preferred tool in Perl for managing chunks of arrays. Consider:

    use strict; use warnings; my $test = ''; while (<DATA>) { if (! /^\d+$/) { $test .= $_; next; } chomp; my $n = $_; my @words = split /\s+/, $test; my @lines; # Use splice to pull out the desired lines push @lines, [splice @words, 0, $n] while @words; print "@$_\n" for @lines; } __DATA__ A huge string separated by lots and lots of words that I'd like to bre +ak up into n shorter strings of length y. This is stored in a __DATA__ secti +on to make a stand alone test program. Note that lines containing only a number provide an 'n' like the 10 on + the following line. 10 Note too that you can add more text that will be added and processed b +y any subsequent 'n' lines. 12

    Prints:

    A huge string separated by lots and lots of words that I'd like to break up into n shorter strings of length y. This is stored in a __DATA__ section to make a stand alone test program. Note that lines containing only a number provide an 'n' like the 10 on the following line. A huge string separated by lots and lots of words that I'd like to break up into n shorter strings of length y. This is stored in a __DATA__ section to make a stand alone test program. Note that lines containing only a number provide an 'n' like the 10 on the following line. Note too that you can add more text that will be added and processed by any subsequent 'n' lines.

    which nicely gets rid of all those nasty C style for loops with their tricksy conditions and increments, and also removes the need for fussy slices and error prone indexes.

    Perl's toolbox is pretty extensive and for some reason splice is often overlooked - it's worth knowing about.


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://613752]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-09-21 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls