New to perl.

MBrown

Diamond Member
Jul 5, 2001
5,726
35
91
Edit: Before you continue, my program now works.


So I am new to perl and I am trying to split a text file in to a hash. It says I have an error on line 10. Anyone know why?

Code:
#!/usr/bin/perl
open (FILE, "name.txt");
print <FILE>;
close(FILE);

my %hash = ();

while(<FILE>){
	($name, $number) = split(/ /, $_);
	$hash($name) = ($number);
}


Edit: Now I have a new issue. My code is supposed to be doing to following. Gets the name of the file from the user, then takes that file and splits its contents into two hashes. Then the code is supposed to have to user enter one of three options. N for name # for number and . for exit. The problem I am running into now is when I enter in a option it just goes to the first argument in the if statement no matter what option I give it. I am not sure what I am missing. Can anyone give me some pointers? Thanks.

Code:
#!/usr/bin/perl

#user selects what file to use
print "File name to read in: "; 
$filename = <>;	
chomp $filename;
open (FILE, $filename);



#this bit of code splits the file into two hashes
while(<FILE>){
	chomp;
	($name, $number) = split(/ /, $_);
	$namehash{$name} = $number;
	$numberhash{$number} = $name;
	}
close(FILE);




#this bit of code is supposed to take the option from the user and do the appropriate action
while (true){
	print "Enter (N) for name and (#) for number search and (.) to exit";
	$option = <>;
	chomp $option;
	if($option == "N"){
		print "Enter number: ";
		$input = <>;
		chomp $input;
		print $namehash{$input};
	}
	elsif($option == "#"){
		print "Enter name: ";
		$input = <>;
		chomp $input;
		print $numberhash{$input};
	}
	elsif($option == "."){
		exit;
	}
	else{
		print "You forgot to enter the option:";
	}
}
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,286
147
106
You're accessing the hash incorrectly. Hashes are accessed with curly braces, not round braces. In other words, $hash{$element} not $hash($element).

BTW,
my %hash = ();

is unnecessary. It could be

my %hash;
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
So I am new to perl
Run! While you still can! :p

Just kidding. I've used Perl for years, and I turned out fine, right?

But seriously, there are a few odd things there. Why print the file's first line? Why then close it before trying to read the rest?

Also, you should generally get rid of the newline before you split(). You want chop, chomp, or s/[\r\n]*$// depending on whether you have Linux/Windows file compatibility issues.
 

Aluvus

Platinum Member
Apr 27, 2006
2,913
1
0
The above answers your question. Some other things:

It's good form, though not explicitly required, to specify whether you want to read or write (or both) from the filehandle when you open it. If not specified, Perl assumes you want to read; but this is not very friendly to others (or yourself) that later look at your code. See perldoc for more. It's also a very good idea to protect yourself from errors when trying to open the file. In short, it's traditional to write something like:

Code:
open (FILE, "< name.txt") or die "Couldn't open file 'name.txt'; $!\n";

$! will contain any error code returned by open().

There's no need to print FILE unless you just want to for whatever reason.

Don't close FILE until you're done using it. You don't absolutely have to close it at all, although it's a good idea; Perl will automatically close it at the end of execution.

When you assign into the hash, there is no reason to put parentheses around $number. Also, using parentheses like that can force list context, which may result in surprises for you.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Run! While you still can! :p

Just kidding. I've used Perl for years, and I turned out fine, right?

But seriously, there are a few odd things there. Why print the file's first line? Why then close it before trying to read the rest?

Also, you should generally get rid of the newline before you split(). You want chop, chomp, or s/[\r\n]*$// depending on whether you have Linux/Windows file compatibility issues.
:) I'm of the opinion that anything perl can do, ruby and python can do better. Yes, perl introduced some really useful stuff (regexes) but it just shows that you can't turn a document language into a programming language without some serious WTFs.

Ruby, IMO, is the language Perl should have been.
 

MBrown

Diamond Member
Jul 5, 2001
5,726
35
91
I had put the close in there because I was testing a bit of code before I started dealing with the hash, I just for got to take it out. Thanks for the help guys.
 

MBrown

Diamond Member
Jul 5, 2001
5,726
35
91
Now I have a new issue. My code is supposed to be doing to following. Gets the name of the file from the user, then takes that file and splits its contents into two hashes. Then the code is supposed to have to user enter one of three options. N for name # for number and . for exit. The problem I am running into now is when I enter in a option it just goes to the first argument in the if statement no matter what option I give it. I am not sure what I am missing. Can anyone give me some pointers? Thanks.

Code:
#!/usr/bin/perl

#user selects what file to use
print "File name to read in: "; 
$filename = <>;	
chomp $filename;
open (FILE, $filename);



#this bit of code splits the file into two hashes
while(<FILE>){
	chomp;
	($name, $number) = split(/ /, $_);
	$namehash{$name} = $number;
	$numberhash{$number} = $name;
	}
close(FILE);




#this bit of code is supposed to take the option from the user and do the appropriate action
while (true){
	print "Enter (N) for name and (#) for number search and (.) to exit";
	$option = <>;
	chomp $option;
	if($option == "N"){
		print "Enter number: ";
		$input = <>;
		chomp $input;
		print $namehash{$input};
	}
	elsif($option == "#"){
		print "Enter name: ";
		$input = <>;
		chomp $input;
		print $numberhash{$input};
	}
	elsif($option == "."){
		exit;
	}
	else{
		print "You forgot to enter the option:";
	}
}

Edit. I figured out that problem. Now the problem I have now is that it is not printing out the value of the hash. No idea why yet.

Edit. Program works...I am so stupid. I forgot to put quotes for my prints!!! Thanks for the help guys.
 
Last edited: