Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Array and Print Value with comma...

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA

Hi,

I need a simple way to print values from an array separated by a comma. Obviously, I don't want a comma after the last value. I know I could do a print join etc... but I need to use a foreach loop..

Code:
my @Array = ('first','second','third','fourth');
	
	foreach my $Value (@Array) {
		print $Value . ",";
	}

Thanks
 
It sounds like you want the join function. Something like:
Code:
print join(',', $var1, $var2, $var3), "\n";
 

isnnid. Because it's massive conditional loop. The code and array in my original post is just an example not my actual code.
 
Hi

Extension said:
I need a simple way to print values from an array separated by a comma. Obviously, I don't want a comma after the last value.
I would change this into "don't want a comma before the first value".
Code:
my @Array = ('first','second','third','fourth');
$comma=0;
foreach my $Value (@Array) {
  print "".($comma?",":"") . $Value;
  $comma=1;
}

Feherke.
 
As has been said, there are a million small ways to attempt this. You have to add a conditional that either aims for the first element or the last. And most likely you'll need at least one new variable, unless you want to process your array by index. Maybe one of these methods will strike your fancy:

Code:
my @Array = ('first','second','third','fourth');

my $delim = '';
foreach my $Value (@Array) {
    print $delim.$Value;
    $delim ||= ","
}

or

Code:
my @Array = ('first','second','third','fourth');

foreach my $i (0..$#Array) {
    my $Value = $Array[$i];
    print ($i ? "," : '') . $Value;
}

- Miller
 
Miller .. when I run your bottom code all I get is ,,,,
and I was trying to figure out this line

print ($i ? "," : '') . $Value;

Now I'm new to the ? operator but have used it in a limited fashion before, what are you trying to evaluate $i against or is it just saying if $i is defined? Or can you explain it a little for me :)
 
Hi

That is an inline [tt]if[/tt], it means [red]if[/red]?[green]then[/green]:[blue]else[/blue] :
Code:
print ([red]$i[/red] ? [green]","[/green] : [blue]''[/blue]) . $Value;
In Perl the number 0 is considered boolean false and all other numbers boolean true. So that expression will return the second expression ( '' ) in the first pass through the loop ( $i==0 ) and the first expression ( "," ) in the next passes ( $i>0 ).

To make it working, force the evaluator to consider all that a string :
Code:
print [red]"".[/red]($i ? "," : '') . $Value;
By the way, I like Miller's first solution. Should be the faster one.

Feherke.
 
travs69 said:
Miller .. when I run your bottom code all I get is ,,,,
and I was trying to figure out this line

Hi travs,

It appears that Feherke has pointed out the flaw in my code and explained the ternary operator quite well. (TY Feherke). If you need more information, you can read about it here:


feherke said:
By the way, I like Miller's first solution. Should be the faster one.

feherke,

I'm pretty sure that it is marginally faster yes, but this is only interesting from a perfectionistic perspective. The comparative evaluation time of a simple conditional to the IO time of a print statement is astronomical. Neverthless, it is entertaining to look into efficiency concerns, as long as we don't sacrifice too much by obfuscating.

- Miller
 
Thank you for the explanation. I have just started using Ternary operater but I find it more and more useful :)
 
Hi

Code:
local($")= "\,";
my @Array = ('first','second','third','fourth');
print "@Array";

dmazzini
GSM System and Telecomm Consultant

 
dmazzini.,

you didn't read the question properly but if a person needed to print an array in scalar context as comma seperated values, that is a good suggestion. But as the OP said:

but I need to use a foreach loop

[wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm a bit confused. With our design heads on, we should be separating the model controller code from the presentation layer (print statement). Wouldn't it be simpler to just
Code:
my @stuffToPrint;

foreach (@inputArray) {
   .
   .
   # massive amount of conditions and code here
   .
   .
   push @stuffToPrint, $something; # finally, at some point
   .
   .
}

print join(",", @stuffToPrint), "\n"; # after [b]rharsh[/b]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top