Forum Moderators: coopster & phranque

Message Too Old, No Replies

Splitting... and counting item numbers...

         

carfac

9:26 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi:

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

VectorJ

10:30 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



You could just split it into an array and access the elements through their index number:


@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];

carfac

10:37 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Vector:

You are the KING! I figured it should be rather easy, I just did not know how! Thanks!

Dave

carfac

10:38 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry- stupid question...

The index number starts at "0"- right? The second item is "1"... correct?

dave

VectorJ

10:46 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



Yep, the index starts at 0. Glad I could help :-)

carfac

11:28 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, Got this ALMOST there- I am having one problem I am not sure how to deal with. It is adding one more loop than there are elements to the list. Here is a list (Note: I am using space-comma-space as a delimiter):

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

Birdman

11:40 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you just need to adjust the comparison operator.

for ($a = 0; $a < scalar(@list); $a++)

$a 'less than', rather than 'less than or equal to'

carfac

11:43 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Birdman:

Thanks- that is all it needed!

Dave