Same goes for @student, $key and your hash. Also * is special in terms of a regexp, so you'll want a \ in front.my $students="students.txt";
You can use parenthasis when defining a has key so no need to create the extra $key variable.#!/usr/bin/perl use strict; use warnings; open(FH, ">students.txt"); print FH "Perry*Steve*234\nSmith*Jane*456\nJones*Mary*567\n"; close(FH); my %hash; my $students = "students.txt"; open(FH, $students) or die("Could not open file!"); while (<FH>) { my ($firstname, $lastname, $password) = split (/\*/, $_); $hash{"$firstname $lastname"} = $password; }#while
Aghhh... I love newbie questions, makes me feel like I know stuff ;)#!/usr/bin/perl use strict; use warnings; open(FH, ">students.txt"); print FH "Perry|Steve|234\nSmith|Jane|456\nJones|Mary|567\n"; close(FH); my %hash; my $students = "students.txt"; open(FH, $students) or die("Could not open file!"); while (<FH>) { my ($firstname, $lastname, $password) = split (/\|/, $_); $hash{"$firstname $lastname"} = $password; }#while
In reply to Re: Open a file and put the contents in a hash with a value
by cosmicperl
in thread Open a file and put the contents in a hash with a value
by trenchwar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |