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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is the rule ?

Status
Not open for further replies.

WebGoat

MIS
Jul 16, 2005
85
US
Code:
#!/usr/local/bin/perl
@food  = ("apples", "pears", "eels");
#$l=push(@food, "eggs", "lard");
#($a, $b) = @food;
#$f ="@food";
#print $b;
#($a, $b) = ($c, $d);
print @food."";

output:
3

by which rule it prnts like this ?

i know a rule which has "." at the append.
the rule is
$a = $b . $c; # Concatenate $b and $c

but the above output does not obey this .
will you please tell me how this output comes ?
 
By using . to join @food with "" you are forcing perl to see @food as a scalar. Any array taken in a scalar context returns the number of elements in that array.
If you look at your array it has 3 elements so perl is functioning correctly.
If you are trying to display the array then why not just print @food;?


Trojan.
 
Also in most cases when you have an array and you want to put something between the elements in it to display you should use join. so something like this if you wanted your list printed space delimited.

Code:
print join(' ', @food);
 
Sorry I should have clarified. I was just showing the join example in case you want to delimit the file with something else. With join you can put whatever you want in between the values. Common uses of this are to make a tab delimited file which becomes very easy to split on if read in by another script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top