Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Perl newbie question


  • Please log in to reply

#1
idrank

idrank

    New Member

  • Member
  • Pip
  • 1 posts
Hi,
I am having a problem reading input from a file into an array. My simple code is:

#***********************************************************
#!usr/bin/perl

open DATA, "<test.dat";


chomp(@strings = <DATA>); #read the lines, not the newlines

print "@strings";

close DATA;

#***********************************************************

where the datafile 'test.dat' contains something simple, something like:

a 5 4 3
b 4 5 8
d 1 4 2
c 0 0 8

All I want to do is read each line into an element of an array, @strings, without the newline characters. When I run the above code, however, it will only print out 'c 0 0 8'. How come it doesn't print out all of the elements? This works fine if the input comes from <STDIN>. What am I missing here?

Thanks
  • 0

Advertisements


#2
BuRnEr

BuRnEr

    Member

  • Member
  • PipPip
  • 48 posts
#!usr/bin/perl

open DATA, "<test.dat";
@strings = <DATA>; #correction
close DATA;
print @strings; #correction

#close DATA;

and run it again ....

take Care
BuRnEr
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP