Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: building Drop down menu - from a flat file

by gryphon (Abbot)
on Jul 24, 2002 at 16:20 UTC ( [id://184924]=note: print w/replies, xml ) Need Help??


in reply to building Drop down menu - from a flat file

Greetings data67,

If it was me, I'd read through the file, splitting each line and pushing a particular element into an array (if you just want each value in the column) or a hash (if you want distinct fields). The below example uses both together. (Don't do this. Just pick one of the two.)

open(INPUT, '< your-input-file.txt') or die "Badness: $!"; my (@instructors_array, %instructors_hash); while (<INPUT>) { my ($id, $name, $class, $instructor) = split(/!/); push @instructors_array, $instructor; $instructors_hash{$instructor}++; } close(INPUT); my @array_with_every_entry_sorted = sort @instructors_array; my @array_with_distinct_entries = sort keys %instructors_hash;

There's probably a way to do this in one line with map, but I'm not merlyn or IO, and I haven't had my first cup of coffee yet.

-gryphon
code('Perl') || die;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://184924]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-24 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found