To expand on what
lhoward pointed out, here's some actual code that you can use (untested):
my %data;
while (<DATA>) {
($data{'name'}[$.],
$data{'address'}[$.],
$data{'city'}[$.])
= split /,/,$_;
}
Then, if you want the address from the third record:
print $data{'address'}[3];
A couple of other points to note:
- $. is a special Perl variable which hold the current line number of the input file. There's no need for $b++ (though some may consider $. to be obfuscated).
- You don't need to escape the comma in your regex.
- Your regex will fail if you have a comma embedded in one of your fields. You should use something like Text::CSV if you do a lot of work with CSV data.
Cheers,
Ovid