I would recommend against naming variables based on dynamic input; that can get you into all sorts of troubles. A better idea (besides using DB files or an RDBMS, of course) would be to have a hash whose values are anonymous array references containing the data you need. As for separating the file data, you need to
split the line on spaces. As far as I understand the question, I think this will work for you:
use strict;
my %users;
open USERS, "<users.txt";
while (<USERS>) {
chomp;
my @line = split / /;
$users{shift @line} = \@line;
}
close USERS;
To get back at the data, you need to dereference the array, i.e. @{$users{$name}}. For example, to get a dump of the users,
print join ' ', ($_, map($_, @{$users{$_}}), "\n") for (keys %users);Of course, you'd be better off using a DB file if you can; flat files require you to reinvent the wheel in a lot of ways. Provided tied hashes properly handle array references (which I'm not sure of; I stick to RDBMSs), this should extend to that pretty easily.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.