in reply to putting text into array word by word

jms53:

Try:

# Use read mode instead of write and append mode open FILE, "<", "input.txt" or die $!; print "file loaded \n"; my @all_words; while (<FILE>) { # the words from *this* line my @words = split('', $_); # add to the complete word list push @all_words, @words; }

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: putting text into array word by word
by jms53 (Monk) on Jan 09, 2012 at 18:30 UTC

    Here is my full code:

    #! /usr/bin/perl -w use strict; my $i = 0; my $element; my @words; my @all_words open FILE, "<", "input.txt" or die $!; print "file loaded \n"; while (<FILE>) { # the words from *this* line @words = split('', $_); push @all_words, @words; } print "table loaded \n"; foreach $element (@words) { print "$element"; }

    the "file loaded" and "table loaded" messages appear, however the elements are not printed afterwards

      You also probably want split(' ', $_); (to split on whitespace), not split('', $_);.  Or simply split; (which has the same effect).

      BTW, your </code > tag doesn't work, because you have a space in between the angle bracket and the tag name...

      jms53:

      Try printing from @all_words instead of @words.

      Update: s/allwords/all_words/, added code tags.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.