Re: foreach statement output



It's gonna be very hard to do what I want to do now based upon what I now 
know about foreach loop workings.  I've got an incoming array of unknown 
length from 1 to 8, each element containing a digit between and including 0 
and 49.  I want to build a long string (50) whose composition is to be a 
zero at each position not corresponding to a digit in the short array, and a 
1 at each position which corresponds to a number in the short array.  Thus, 
the long string will be a code for what's in the short array, and I can use 
it in a hash I'm buiding and wish to use in reverse.  If, for example, my 
incoming array is (0 8 9 32 50), my string will be 
"10000001100000000000000000000001000000000000000001"

Please don't send me solutions, because I want to solve my problem and learn 
from it.  My solution will involve getting the first element of the short 
array, planting it into the string somehow, and then using pop until I run 
the array dry.

I appreciate very much the great advice I've been getting here.  I do have 
access to the four main O'Reilly books I know:

Learning Perl
The Perl Cookbook
Programming Perl
CGI Programming

My big confusion over this foreach issue is I failed to realize that the 
loop variable becomes each element of the array in succession, and also that 
alphabetic variables are undef and act as zeroes.

J.K.


"Glenn Jackman" <glennj@xxxxxxxxxxxxxxxxxxx> wrote in message 
news:slrndu1om4.5bi.xx087@xxxxxxxxxxxxxxxxx
> At 2006-02-01 12:31AM, mike <rallabs@xxxxxxxxxxxx> wrote:
> [,...]
>>  my @small=(2,3);
>>  foreach my $s(@small){
>>  print $co->h4("loop variable is $s; small[s] is $small[$s]");
>>  $s += 1;
>>  }
>>  print $co->h4("small=@small");
>>
>>  OUTPUT:
>>  small=2 3
>>  loop variable is 2; small[s] is
>>  loop variable is 3; small[s] is
>>  small=3 4
>>
>>  This out put doesn't make any sense to me.  I think for the numeric case
>>  it's looking for elements #2 and #3 which of course don't exist since 
>> the
>>  array has only teo elements, with indices 0 and 1.  Maybe this is why I 
>> get
>>  no output for the prints of $small[$s] for both values of $s?
>
> You're right.
>
> It may be easier to understand if you don't use numeric data:
>
>    my @array = ('apple','orange','banana');
>    foreach my $fruit (@array) {
>        print "loop var is $fruit; array[$fruit] is $array[$fruit]\n";
>    }
>
> Does it make sense to ask for the "apple"-th element of an array?
>
> (However, what perl does in this case is to look at "apple" as a number
> and it implicitly turns it into the number 0.  Use warnings indicates:
>    Argument "apple" isn't numeric in array element
> Use diagnostics says:
>    (W numeric) The indicated string was fed as an argument to an
>    operator that expected a numeric value instead.  If you're fortunate
>    the message will identify which operator was so unfortunate.
> }
>
> I would recommend a good book at this point to help you learn Perl,
> perhaps "Learning Perl" (http://learn.perl.org/)
>
> Also in your loop, you increment the loop variable.  As you can see from
> your output, that alters the array you're looping over.  That's why the
> loop variable is called an alias -- if you modify it, you modify the
> corresponding array element.
>
> -- 
> Glenn Jackman
> Ulterior Designer
>
> -- 
> PLEASE NOTE: comp.infosystems.www.authoring.cgi is a
> SELF-MODERATED newsgroup. aa.net and boutell.com are
> NOT the originators of the articles and are NOT responsible
> for their content.
>
> HOW TO POST to comp.infosystems.www.authoring.cgi:
> http://www.thinkspot.net/ciwac/howtopost.html
> 



-- 
PLEASE NOTE: comp.infosystems.www.authoring.cgi is a
SELF-MODERATED newsgroup. aa.net and boutell.com are
NOT the originators of the articles and are NOT responsible
for their content.

HOW TO POST to comp.infosystems.www.authoring.cgi:
http://www.thinkspot.net/ciwac/howtopost.html

.



Relevant Pages

  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • parse hash by array element seems to run slow
    ... I've been through the faq's and every web site that focuses on perl ... I iterate the array and search the hash keys to find a match and then ... foreach $verifyArr ...
    (comp.lang.perl.misc)
  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)
  • [DGBI] oops, obviously Ive missed something (was: regexp or array?)
    ... foreach { ... Better use an array of precompiled regexpes: ... perl -wle ' ... use Benchmark qw|cmpthese timethese|; ...
    (comp.lang.perl.misc)
  • Re: Strange behaviour
    ... Personally, I would use the foreach version, as it is easier to read. ... bool AllNegative{ ... besides the difference between for and foreach the first example will always test each element of the array while the second example only tests until it finds the first non-negative. ... This is independant of for/foreach since also a for loop can be exited with return ...
    (microsoft.public.dotnet.languages.csharp)