Gossamer Forum
Home : General : Perl Programming :

$category_name array limiting/cutting

Quote Reply
$category_name array limiting/cutting
I'm wondering how do you get part of the field of an array for a variable. I've finally got a perl book book but this is still way over my head Frown looking forward to really study during the winter holidays. Smile

for example in $category_name it has several fields when fully viewed.

ice cream/cream/chocolate/rocky road
or
ice cream/ice milk/mixed/neopolitan

How do I get just one part of this variable???? for example just the 3rd variable (chocolate or mixed) .

I've found code that lets you split the variable down to first place value but don't know have to change it Frown

CATEGORY: foreach $cat (sort keys %category) {
next unless ($cat =~ m,^([^/]*)$,);
$title = &build_unlinked_title ($cat);
@categorylist = split (/\//, $cat);
$output .= qq~<option value="$cat">$title</option>~;
}

actually this comes from the search_box mod and it works for drop down categories but I was wondering if this can be used to do what I was asking about. the split perl function is still beyond what I've been studied so far. (still too advanced for me)

what did frost say

"But I have perl to study, And miles of perl to go before I sleep, And miles of perl to go before I sleep."


I'd really appreciate any help. thanks

Quote Reply
Re: $category_name array limiting/cutting In reply to
 
Yes, You Do Want to use split, Weather you like it or not, Just kidding,

Like so,

$variable = "ice cream/ice milk/mixed/neopolitan";
(@flavor)=split(/\//, $variable);
print "$flavor[2]";

The output being, chocolate or mixed.

You see, I put the flavors which are separated by "/" into a variable so I can work with it. Then I assigned all of the peices of the variable into @flavor, an array. By pieces I mean the fields that are split by a "/". Then when I wanted to print the third one, I printed $flavor[2], 2 not 3 since perl starts at 0. As for "split(/\// ..." the // holds the character(s) that are going to split the $variable. in this case a "/". But a "/" is the same character as the two characters that define the deliminater themselves. So we want to put a "\" (escape) in front of it so the perl will interpret it properly. Not to tough that split now is it. ;)

That's it for my 3 am discussion,

Laters,

perlkid