I want to add something where I take a list and split it, but I also need to know the item number in order, from the list. In Perl. Basically, I have this data:
Item 1, Item 2, Item 3
OK, so I want to split it at the "comma-space", and turn it into:
<A HREF="http://www.myserver.com/script.cgi?number=1">Item 1</A>, <A HREF="http://www.myserver.com/script.cgi?number=2">Item 2</A>, <A HREF="http://www.myserver.com/script.cgi?number=3">Item 3</A>
Further down the script, I also want to be able to extract JUST the corresponding data... that is, if "number=2" is sent, I want to be able to pull JUST "Item 2"
Can anyone give me a couple hints?
Thanks!
Dave
@list = split (/,\s/, $list);
for ($a=0; $a<=scalar(@list); $a++) {
print "<A HREF=\"http://www.myserver.com/script.cgi?number=$a\">Item $a</A>";
}
and later just access directly by the index number:
$selected_item = $list[$number];
Mission To Mariposa , The Germ Bomb , Even Dictators Die
Here is what I get returned:
<A HREF="http://www.myserver.com/script.cgi?number=0">Mission To Mariposa</A>, <A HREF="http://www.myserver.com/script.cgi?number=1">The Germ Bomb</A>, <A HREF="http://www.myserver.com/script.cgi?number=2">Even Dictators Die</A>, <A HREF="http://www.myserver.com/script.cgi?number=3">h</A>, <A HREF="http://www.myserver.com/script.cgi?number=4"></A>
Here is the code I am using:
@list = split (/\s,\s/, $short);
for ($a=0; $a<=scalar(@list); $a++) {
$selected_item = $list[$a];
$output.= "<A HREF=\"http://www.myserver.com/script.cgi?number=$a\">$selected_item</A>, ";
}
chop($output);
chop($output);
(the final chops kill the last ", " from output...)
Dave