School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,103 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,037 people online right now. Registration is fast and FREE... Join Now!




File IO in Perl

 

File IO in Perl

Mila

29 Oct, 2009 - 06:42 PM
Post #1

D.I.C Head
Group Icon

Joined: 28 Oct, 2006
Posts: 105


Dream Kudos: 25
My Contributions
Hi all, Perl newbie here with a very newbie-ish question.

So I'm writing a small program to read a file and print to the screen. The file, called abbrevs.abr, is a text file with the following lines of text:
CODE
United States,US
Poland,PL
United Kingdom,UK
Canada,CA


My code is as follows:
perl
#!/usr/bin/perl -w
print "What is the filename? ";
$filename = <STDIN>;

# load abbreviations
open(ABBR, $filename) || die ("Could not open file.");
@raw_data = <ABBR>;
close(ABBR);
print "The abbreviation list:\n";
foreach $abbreviation (@raw_data) {
chomp($abbreviation);
($fullname, $abbrev) = split(/,/, $abbreviation);
print "$fullname abbreviates to $abbrev\n";
}


What it should do is print a series of lines that says something like "United States abbreviates to US" and so forth.

It doesn't.

It properly prompts for the filename, the file is in the same folder as the .pl file, it prints "The abbreviation list:" and then finishes. No errors, no nothing.

It's probably a very stupid little error, since I'm trying to teach myself this language out of a book that's 15 years old and the internet.

I also tried this, just to straight out print the information from the file:
perl
#!/usr/bin/perl -w
print "What is the filename? ";
$filename = <STDIN>;

# load abbreviations
open(ABBR, $filename);
print "The abbreviation list:\n";
while (<ABBR>) {
chomp;
print "Line: $_\n";
}


That gave the same result as the other. What am I doing wrong? Since I'm running Xubuntu, I thought maybe I'd have to run it as root/sudo, but that made no difference either.

Thanks in advance,
Mila

This post has been edited by Mila: 29 Oct, 2009 - 06:42 PM

User is offlineProfile CardPM
+Quote Post


no2pencil

RE: File IO In Perl

29 Oct, 2009 - 06:46 PM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,492



Thanked: 303 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
Are you actually trying to open a file? Because you have standard in for the file name...

Maybe if you replace this line :

CODE
$filename = <STDIN>;


With the text name of the file ...

Read a file, display the results
User is online!Profile CardPM
+Quote Post

Mila

RE: File IO In Perl

29 Oct, 2009 - 06:51 PM
Post #3

D.I.C Head
Group Icon

Joined: 28 Oct, 2006
Posts: 105


Dream Kudos: 25
My Contributions
Well that's what traps the user's input, yes? So the user could type "abbrev.abr" and that's what would be saved as $filename... or am I totally wrong?

I also tried hard-coding the filename in, and that didn't work either.

And I tried the code from this snipper, substituting my filename for "data.txt" and that didn't work either. Is it maybe a problem with my perl installation?

This post has been edited by Mila: 29 Oct, 2009 - 06:54 PM
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: File IO In Perl

30 Oct, 2009 - 04:52 AM
Post #4

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 204



Thanked: 36 times
My Contributions
QUOTE(Mila @ 30 Oct, 2009 - 02:42 AM) *

perl
open(ABBR, $filename) || die ("Could not open file.");


You've forgotten to tell Perl what to do with the file (read/write/append) that you're opening. Try
CODE
open(ABBR, '<', $filename) || die...
instead.

Also take a look at my reply to CatchThi5Drift's question at http://www.dreamincode.net/forums/showtopic134769.htm for a lot of additional general Perl advice, most of which applies to the code you've posted here, too.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:31PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month