Gossamer Forum
Home : General : Perl Programming :

Extracting from an array

Quote Reply
Extracting from an array
I would appreciate some help with this: I have a field called <%Pages%> which includes a series of numbers separated by commas (ie. 45,567,13,6). I wish to extract each number so as to use them all in a HTML template. I thus wrote a global with my limited perl experience: my global is named <%pages%>

sub {
my $index = shift;
# get field, split into an array
my ($tags) = GT::Template->tags;
my (@pages) = split /,/, $tags->{Pages};

# just to debug: shows I'm extracting correctly!
my $numberpages = @paras;

# initiate loop to output each element of the array
for ($index=0; $index<@pages; $index++) {
return @pages[$index];
}
# end sub
}

In my HTML, I just want to do a simple loop to show each number:
for page=first to page=last
print page <BR>
next

It's the perl that's not correct. I only have the first number returned. But I very much suspect that there is a far more elegant way of achieving my objective! Thanks for some expert advice!
Quote Reply
Re: [charly] Extracting from an array In reply to
You can only "return" once from a subroutine. You have your "return" inside a for() loop so as soon as "return" is hit for the first time, the loop ends.

You also use:

my $index = shift;

...but then in your for() loop you set $index to 0.

Thirdly I think you want

$pages[$index];

..and not...

@pages[$index];

...they are very different.

Something like this would work as a template loop tag...

Code:
sub {
my $tags = GT::Template->tags;
my $pages = $tags->{Pages};
return { page_loop => [ map +{ page => $_ }, split /,/, $pages ] };
}

In your template you'd do something like:

Code:
<%my_global%> <!-- Init the loop tag -->
<%loop page_loop%>
Page: <%page%><br>
<%endloop%>
Quote Reply
Re: [Paul] Extracting from an array In reply to
Thanks enormously Paul - a very quick reply and it works (of course!) Your tops! Cheers
Quote Reply
Re: [Paul] Extracting from an array In reply to
Postscript Paul: this is how I've implemented it in my template:
<%get_related%> <!-- Init the loop tag -->
<%loop page_loop%>
<a href="db.cgi?do=search_results&keyword=*&db=<%db%>&RecordID=<%page%>&details=1">Related page</a><br>
<%endloop%>

Would it be possible to replace the 'Related page' in the URL by the page's actual Title? (Each page record has a field <%Title%>)
Quote Reply
Re: [charly] Extracting from an array In reply to
Yep, you'd just need to select the title and pass in a tag, so change:

page => $_

to

page => $_, title => $table->select('Title')->fetchrow

Add:

my $table = $DB->table('your_table');

...near the top.
Quote Reply
Re: [Paul] Extracting from an array In reply to
Sorry, I don't follow you. My global is thus:

sub {
my $table = $DB->table('Articles');
my $tags = GT::Template->tags;
my $pages = $tags->{Related};
return { page_loop => [ map +{ page => $_, title => $table->select('Title')->fetchrow}, split /,/, $pages ] };
}

my template is thus - but I expected it to be wrong!
<%get_related%> <!-- Init the loop tag -->
<%loop page_loop%>
<a href="db.cgi?do=search_results&keyword=*&db=<%db%>&RecordID=<%page%>&details=1"><%Title%></a><br>
<%endloop%>

But this <%Title%> in the URL gives me the referring page's Title and not the Titles of the records. Trying with <%title%> gives EACH time the Title of a record whose number is NOT in the array. I've missed something...
Thanks

Last edited by:

charly: May 2, 2003, 1:49 PM
Quote Reply
Re: [charly] Extracting from an array In reply to
Change:

$table->select('Title')->fetchrow

to...

$table->select('Title', { ID => $_ })->fetchrow

Change ID to whatever the key name is for each page.
Quote Reply
Re: [Paul] Extracting from an array In reply to
Thank you very much indeed. Now works to perfection. And with your code, I've added to my perl knowledge! Cheers
Quote Reply
Re: [charly] Extracting from an array In reply to
Paul, is it possible to extend this global one step further, ie. to return TWO fields corresponding to one record. Actually I have the global <%get_related%> :

sub {
my $table = $DB->table('Articles');
my $tags = GT::Template->tags;
my $pages = $tags->{Related};
return { page_loop => [ map +{ page => $_, Title => $table->select('Title', {RecordID => $_})->fetchrow}, split /,/, $pages ] };
}

In my template, I have :
<%get_related%>
<%loop page_loop%>
<%Title%>
<%endloop%>

I would like to have :

<%get_related%>
<%loop page_loop%>
<%Title%> - <%DatePub%>
<%endloop%>

where DatePub is like Title a field in the same table Articles? My mastery of perl, although improving, isn't up to this modification! Thanks for your help.
Quote Reply
Re: [charly] Extracting from an array In reply to
Try:

Code:
sub {
my %hash = ();
my @loop = ();
my $table = $DB->table('Articles');
for (split /,/, $_[0]->{Related}) {
@hash{qw/Title DatePub/} = $table->select(['Title', 'DatePub'], {RecordID => $_})->fetchrow;
push @loop, { page => $_, %hash };
}

return { page_loop => \@loop };
}
Quote Reply
Re: [Paul] Extracting from an array In reply to
Thanks a lot Paul ! works fine, and after understanding code, I've extrapolated the global further to retrieve three fields:


sub {
my %hash = ();
my @loop = ();
my $table = $DB->table('Articles');
for (split /,/, $_[0]->{Related}) {
@hash{qw/Title Titre DatePub/} = $table->select(['Title', 'Titre', DatePub'], {RecordID => $_})->fetchrow;
push @loop, { page => $_, %hash };
}

return { page_loop => \@loop };
}

Much appreciate your help!
Quote Reply
Re: [Paul] Extracting from an array In reply to
Hi!
Yet again, I have seen the potential of the above global with a further extrapolation. It presently parses a field (containing of a series of number-IDs separated by commas) in ONE table and returns the contents of certain fields for each of these number-ID in this SAME table.

Would it be possible to extract the content of certain fields for each of these number-ID in a SEPARATE table? I hope I am precise enough in my request. In fact the number-IDs in the first table are keys to fields in the second table.
Thanks for any suggestions!
Charly

Last edited by:

charly: Jul 13, 2003, 6:46 AM