in reply to Open a file and put the contents in a hash with a value
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Open a file and put the contents in a hash with a value
by trenchwar (Beadle) on Apr 11, 2008 at 22:11 UTC | |
by oko1 (Deacon) on Apr 12, 2008 at 04:03 UTC | |
by trenchwar (Beadle) on Apr 12, 2008 at 18:13 UTC | |
|
Re^2: Open a file and put the contents in a hash with a value
by trenchwar (Beadle) on Apr 11, 2008 at 22:13 UTC |