Gossamer Forum
Home : General : Perl Programming :

Help on replacing items in arrays

Quote Reply
Help on replacing items in arrays
I'm learning the Perl so recently trying to replacing the last item in a array statements.

Here is the code :

for (0 .. $numhits - 1) {
%rec = &array_to_hash($_, @hits);
$output .= " '$rec{'title'}',\n";
}
I just want to remove the comma " , " sign after '$rec{'title'}', so it will print out the $rec{'title'} without any comma " , " for the last array.

Could anyone help me out ?

Any replies would be appreciated.

Thanks.

Regards,
Tommy
Quote Reply
Re: Help on replacing items in arrays In reply to
Hi Tommy:

After the FOR loop, add the following line:

chop $output;

It will strip off the last character, the comma.


Dan Cool

LotusLand
Vancouver, BC, Canada
Top Ranked City in World for Quality of Life
Quote Reply
Re: Help on replacing items in arrays In reply to
Thanks Dan,

But unfortunately it chops the "\n" and doesn't remove the comma, so I get a long line instead.

Any Idea ?


Regards,
Tommy
Quote Reply
Re: Help on replacing items in arrays In reply to
chomp removes \n, chop should remove the last character.

Try.....

chomp;
chop;

$output .= "\n";

Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/

Quote Reply
Re: Help on replacing items in arrays In reply to
I don't see you using an array.. %rec is a hash.

Why not loop through and instead of building $output, push each $rec{title} into an array? Then when you have them all you can just do:

$output = join (", \n", @your_array) . "\n";

--mark

Quote Reply
Re: Help on replacing items in arrays In reply to
Thanks Paul And Mark for your quick reply,

Paul, I dont want to remove the "\n" character, just only the comma before the "\n".
Maybe I'm not make my self clear, from this code, say that I get 5 arrays of item:

for (0 .. $numhits - 1) {
%rec = &array_to_hash($_, @hits);
$output .= " '$rec{'title'}',\n";
}

It will print out an arrays:
'Song title 1',
'Song title 2',
'Song title 3',
'Song title 4',
'Song title 5',

The only things is to remove the last comma in the 'Song title 5', so on the other hand it will printout an arrays of:
'Song title 1',
'Song title 2',
'Song title 3',
'Song title 4',
'Song title 5'

That's it.

And for Mark, sorry I couldnt follow you, could you give me an examples ?

Thank you



Regards,
Tommy
Quote Reply
Re: Help on replacing items in arrays In reply to
Hi I know, but Im sure the code I gave will achieve what you want.....

chomp;
chop;

$output .= "\n";


chomp; will remove the \n then chop; will remove the comma and then the \n is re-appended to $output; so it should work.......did you try?

Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/

Quote Reply
Re: Help on replacing items in arrays In reply to
Code:
for (0 .. $numhits - 1) {
%rec = array_to_hash($_, @hits);
push (@foo, qq/'$rec{'title'}'/);
}

$output = join (", \n", @foo) . "\n";
--mark

Quote Reply
Re: Help on replacing items in arrays In reply to
Hi Mark:

Instead of creating the second array, would it not be more efficient to maintain his code but after the loop, add:
$output =~ s/,$//;


Dan Cool

LotusLand
Vancouver, BC, Canada
Top Ranked City in World for Quality of Life
Quote Reply
Re: Help on replacing items in arrays In reply to
No, it's actually much faster to use 1 join instead of several concatenations as it is; using a regexp to remove the last comma would just make an even larger (although not significantly larger) difference.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: Help on replacing items in arrays In reply to
Thanks Paul,

Yes, Ive tried it, however it's still print the comma.
If I put it inside the looping ( before the right bracket ), it gives a spce between the arrays.

Anyway I do appreciated it, because Ive learned something new with your solutions.

Regards,
Tommy
Quote Reply
Re: Help on replacing items in arrays In reply to
Thanks Mark,

Yes, it's working, but I want the " ' " character also to be printout.
Precisely as above examples :
'Songtitle 5'

How do I do that ?

Regards,
Tommy