Click to See Complete Forum and Search --> : Perl syntax (i'm being lazy)


trashthing
08-29-2003, 10:48 PM
hi. would you give me the Perl syntax for reading a text file and taking each character and assigning it to a varible? i know how to do this, but i don't know the syntax. the Perl book i have sucks, and i don't feel like searching google.

Hayl
08-29-2003, 10:55 PM
open FILE, "filename";

while ( <FILE>) {push(@contents, $_) }

eep! just reread your post. you want it by character and i gave you by line :) you get the idea though. can't mae it too easy for you :)

(PS: have you looked at permoks.org yet? its an AWESOME site for learning perl and getting help.

just4spam
08-30-2003, 01:41 AM
#! /usr/bin/perl

open (FILE, "/perl/testtk.pl") || die "Can't open file: $!";
for ($i=0; $i = getc(FILE); $i++) {
print "I is $i\n"; # or do something else
}

o0zi
09-01-2003, 02:51 AM
You could also do:

#!/usr/bin/perl -w
open(INFILE, "test.pl") || die "Cannot open file.";
while(defined($ichar = getc INFILE)) {
print $ichar; #Or do whatever with it
}
close INFILE;

If you're going to run this code and the code that other people have suggested in Windows, you'll have to use binmode.

Perlmonks is a great site. You learn Perl fast, Hayl:)