Re: foreach statement output
- From: "mike" <rallabs@xxxxxxxxxxxx>
- Date: Thu, 2 Feb 2006 18:59:59 CST
My solution is longer than 3 lines, but maybe its length could be decreased
by using a 'for' statement instead of the foreach loop:
my $s;
my @BIGG=(0) x 50; # sets elements 0-49 to zero
my @small=qw*0 1 8 9 32 49 40*; #incoming array
print $co->h4("small=@small");
print $co->h4("BIGG=@BIGG");
foreach my $s(@small){
$BIGG[$s] = 1;}
print $co->h4("BIGG is now @BIGG");
This script does the job, and I will use the array to generate a string.
There will be up to 50 of these strings, and I want to find a way to
eliminate duplicates among these 50 strings. Also, I've got to do a test in
the above script to make sure $s is never above 49. My earlier (last night)
message was wrong; it had the number 50 in the short array.
Thanks to everybody for the advice so far.
Jen
"Paul Lalli" <mritty@xxxxxxxxx> wrote in message
news:1138886871.222738.327300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
mike wrote:
It's gonna be very hard to do what I want to do now based upon what I now
know about foreach loop workings.
Why? This whole thread, you've been trying to use the foreach-style
loop as though it was a C-style for loop. If you want to use the for
loop, use the for loop. Perl provides both:
for (my $i = 0; $i < @foo; $i++) {
# work with $foo[$i]
}
foreach my $elem (@foo) {
#work with $elem
}
I've got an incoming array of unknown length from 1 to 8,
Be careful with your terminology. "length" is a function that gives us
the number of characters in a string. When talking about the number of
elements in an array, we refer to the array's *size*.
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 won't give you the solution, as you requested, but I will comment
that I see no reason to use 'pop' at all. Nor do I see why Perl's
foreach-loop semantics would cause a problem here. If anything, a
foreach loop will be more helpful than a for loop.
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
All great books. Once you've progressed a little further, allow me to
recommend "the Alpaca", which is currently titled "Learning Perl
Objects References & Modules", but will be renamed "Intermediate Perl"
this March I believe. It picks up right where Learning Perl leaves
off.
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.
Alphabetic values are not undef at all. Any string used in numeric
context is treated as a zero if that string does not start with a
character sequence that could be converted to a number. If it does,
the string is treated as the largest number that could be read from the
start of the string:
'abc' ==> 0
'42bc' ==> 42
'4abc39093' ==> 4
'-532.3219af' ==> -532.3219
Paul Lalli
P.S. For what it's worth, my solution to your problem is exactly three
lines, including the definition of the array, the intial value of the
string, and the modification of the string to its final value.
--
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
.
- Follow-Ups:
- Re: foreach statement output
- From: Paul Lalli
- Re: foreach statement output
- References:
- Re: foreach statement output
- From: mike
- Re: foreach statement output
- From: Glenn Jackman
- Re: foreach statement output
- From: mike
- Re: foreach statement output
- From: Paul Lalli
- Re: foreach statement output
- Prev by Date: Re: foreach statement output
- Next by Date: Re: foreach statement output
- Previous by thread: Re: foreach statement output
- Next by thread: Re: foreach statement output
- Index(es):
Relevant Pages
|