in reply to Re^3: Word density
in thread Word density

The code I am running is
#!/usr/bin/perl use warnings; use strict; my $content = qq(Three blind mice. Three blind mice. See how they ru +n. See how they run. The butcher's wife came after them with a knif +e, three blind mice.); my @words = split(/\s+/, $content); my @words_bi; foreach (@words) { print "$_\n\n"; } foreach (0..$#words) { next if $_ < 1; push(@words_bi, [ @words[$_-1 .. $_] ] ); } foreach (@words_bi) { print "$_\n\n"; }
Which is resulting in
C:\Documents and Settings\admin\Desktop>perl words.pl Three blind mice. Three blind mice. See how they run. See how they run. The butcher's wife came after them with a knife, three blind mice. ARRAY(0x225a20) ARRAY(0x1853c64) ARRAY(0x184780c) ARRAY(0x18477dc) ARRAY(0x18477ac) ARRAY(0x184777c) ARRAY(0x18475c0) ARRAY(0x1847590) ARRAY(0x1847560) ARRAY(0x1847530) ARRAY(0x1847500) ARRAY(0x18474d0) ARRAY(0x18474a0) ARRAY(0x1847470) ARRAY(0x1847440) ARRAY(0x1847410) ARRAY(0x18473e0) ARRAY(0x18473b0) ARRAY(0x1847380) ARRAY(0x1847350) ARRAY(0x1847320) ARRAY(0x18472f0) ARRAY(0x18472c0) ARRAY(0x1847290) ARRAY(0x184720c) C:\Documents and Settings\admin\Desktop>
Thanks!

Replies are listed 'Best First'.
Re^5: Word density
by sulfericacid (Deacon) on Mar 19, 2006 at 19:36 UTC
    Perhaps this will do what you want?
    push(@words_bi, $words[$_-1] . $words[$_] );
    Update: Oops, the words will be stuck together. My bad.
    push(@words_bi, $words[$_-1] . " " . $words[$_] );


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
Re^5: Word density
by ikegami (Patriarch) on Mar 21, 2006 at 01:26 UTC
    foreach (@words_bi) { print join("\n", @$_), "\n\n"; }

    Read up on arrays of arrays (AoA) in perllol.