Stumped by formatting - newbie plea

P

Peter Stokes

I'm trying to use a list, created from an array, to build a dropdown
list in a web page form.

I start with a flatfile database, pipe delimited. The first field is
the county, then the name, then other info. The dropdown list is to be
of the counties.

I've got:
open (INFILE, "db1.dat");
while (<INFILE>) {
@things = split /\|/;
@counties = splice(@things,0,1);

This is fine, it generates my list, but of course it has duplicates in
it. So to get rid of the duplicates I've got:
@counties = grep( ( ($h{$_}++ < 1) || 0 ), @counties );
Which does indeed get rid of the duplicates.

This is where I'm having the problem. If I now:
print @counties;
I get a solid line of text. I need to format this. If I:
print "<option value=\"@counties\">@counties\n";
I get a list which includes formatting on the lines that have been
removed, so the result looks like:
<option value="Berkshire">Berkshire
<option value="Dorset">Dorset
<option value="">
<option value="">
<option value="Gloucestershire">Gloucestershire
....and so on.

Please can anyone suggest where I'm going wrong.

Thanks

Peter Stokes
 
G

Gunnar Hjalmarsson

Peter said:
So to get rid of the duplicates I've got:
@counties = grep( ( ($h{$_}++ < 1) || 0 ), @counties );
Which does indeed get rid of the duplicates.

If I:
print "<option value=\"@counties\">@counties\n";
I get a list which includes formatting on the lines that have been
removed, so the result looks like:
<option value="Berkshire">Berkshire
<option value="Dorset">Dorset
<option value="">
<option value="">
<option value="Gloucestershire">Gloucestershire
...and so on.

http://www.perldoc.com/perl5.8.0/po...move-duplicate-elements-from-a-list-or-array-
 
T

Tad McClellan

Peter Stokes said:
Subject: Stumped by formatting - newbie plea
^^^^^^^^^^^

Have you seen the Posting Guidelines that are posted here frequently?

Carefully choose the contents of your Subject header
You have 40 precious characters of Subject to win out and be one of
the posts that gets read. Don't waste them. Take care while
composing them, they are the key that opens the door to getting an
answer.

Spend them indicating what aspect of Perl others will find if they
should decide to read your article.

Do not spend them indicating "experience level" (guru, newbie...).

Do not spend them pleading (please read, urgent, help!...).

...

open (INFILE, "db1.dat");


You should always, yes *always*, check the return value from open():

open (INFILE, 'db1.dat') or die "could not open 'db1.dat' $!";

If I:
print "<option value=\"@counties\">@counties\n";


What does @countries contain at this point?

I get a list which includes formatting on the lines that have been
removed, so the result looks like:
<option value="Berkshire">Berkshire
<option value="Dorset">Dorset
<option value="">
<option value="">
<option value="Gloucestershire">Gloucestershire
...and so on.


I don't see how the code above can produce the output you claim.

Do you have a short and complete program that we can run that
illustrates the problem you are having?

If you can do that, then we can surely answer your question...

Please can anyone suggest where I'm going wrong.


Not showing us Real Code.
 
Z

Zak McGregor

On Fri, 25 Jul 2003 01:20:51 +0200, Peter Stokes <"Peter Stokes"

[snip]

Others have already pointed you to the correct way of eliminating
duplicates from an array. The method I snipped was not going to work.
This is where I'm having the problem. If I now:
print @counties;
I get a solid line of text. I need to format this. If I:
print "<option value=\"@counties\">@counties\n";
I get a list which includes formatting on the lines that have been
removed, so the result looks like:
<option value="Berkshire">Berkshire
<option value="Dorset">Dorset
<option value="">
<option value="">
<option value="Gloucestershire">Gloucestershire
...and so on.

Um, no you don't.
Please can anyone suggest where I'm going wrong.

Maybe one of the other guys has a better solution, but I'd be looking at
map right now if I were you.

perldoc -f map

HTH

Ciao

Zak
 
P

pkent

I'm trying to use a list, created from an array, to build a dropdown
list in a web page form.

I start with a flatfile database, pipe delimited. The first field is
the county, then the name, then other info. The dropdown list is to be
of the counties.

I've got:
open (INFILE, "db1.dat");
while (<INFILE>) {
@things = split /\|/;
@counties = splice(@things,0,1);

This is fine, it generates my list, but of course it has duplicates in
it. So to get rid of the duplicates I've got:
@counties = grep( ( ($h{$_}++ < 1) || 0 ), @counties );
Which does indeed get rid of the duplicates.


Personally I often us a hash for this kind of de-duplicating task:

# example code...
foreach(@list_of_things) {
$hash{$_} = 1;
}
foreach(sort keys %hash) {
print "$_\n";
}

You may want to alter the sorting method if the things come out in an
order that isn't right for you - that's easy to do.

P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top