To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.off-topic.geekOpen lugnet.off-topic.geek in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Off-Topic / Geek / 3603
3602  |  3604
Subject: 
Re: Access a LEGO set DB with your phone!
Newsgroups: 
lugnet.off-topic.geek
Date: 
Mon, 18 Mar 2002 18:00:39 GMT
Viewed: 
262 times
  
In lugnet.off-topic.geek, Christopher L. Weeks writes:
The example at
http://www.perldoc.com/perl5.6/faq/perlfaq4.html#How-do-I-process-modify-each-e
lement-of-an-array- suggested using :

   for (@lines) {do stuff}

But that didn't seem to work.  I'm looking at a simple tutorial on foreach, but
I don't get what the $set means or does for the loop.  In your example, is it
doing something like setting $set equal to $sets[each] while stepping through
each array element?  Or is $set just a counter while incrementing along the
array's indecies?

well, $set gets assigned each element in the array, in order.  so if your
array is "1", "10", "20", "this-string", $set will get each of these values,
then run the code block.

Oh yeah.  Oops.  That was a hold over from an attempt to create the result of
split() as an anonymous array.  There was a [ to the left of split at one time
too.  Is there a way to put the results of the split function into a referenced
array?

maybe I'm misunderstanding again...

@array = split /\t/, $test;

will put the result in an array.

if you want to skip a step, and get the result in an annonymous array, you
do something like this:

$ref = [ split /\t/, $test ];

It actually never gets there.  Something else is killing the script before it
returns the results.  I'm doing this by FTPing the code to a cgi-bin space that
I can't ssh or telnet into and then running the CGI through a web page.  Will
that wait for all the results before displaying anything?

it was probably a syntax error.  I would suggest installing perl on your
local box (if it's windows, you can go to activestate.com to download their
perl...  or install it as part of cygwin, the unix for windows package, from
cygwin.com).  that way, you can debug the script before you upload it to the
server... especially if you don't have access to the server error log.

for ($i=0;$i<$#sets;$i++) {....}

How does $#sets evaluate?

$#sets will give you the index of the last element in the array (under any
sane conditions).

my %set_data = ();  # empty hash

declaring this variable is optional, right?  I can just start using
variables/datastructures and they're automagically declared..?

you can, but it's good practice to declare them.  just a habit, I guess.

foreach $set (@sets) {
my $array_ref = [];  # a reference to an annonymous array - new one created
                      # in each iteration of the foreach
@$array_ref = split /\t/, $set;

Is that the right syntax?  are there supposed to be curly braces around the
$array_ref (ie. @{$array_ref} )?  Is that optional, or am I just mistaken?

I believe that would work, but adding the curly braces will defenitly not
hurt :)  like I said, this is really untested.  I know that oft you can get
away with @$ref instead of @{$ref}.

my $set_id = shift @$array_ref;  # the first entry is the set number, we'll
                                  # use that as the indexing key
$set_data{$set_id} = $array_ref; # store our parsed array in the hash
}

That shift pulls off the first cell of the array which isn't unique.  Isn't
that going to mess up searches?  If you pop the last cell instead, that'll use
the guide's unique identifier as the key.  What are the dangers of non-unique
keys?

actually, shift pulls off the first cell of the array.  period.  the whole
unique part is in the hash...  in this case, if the Guide list had two sets
that were 3033-1, the latter would overwrite the former.  But I believe the
Guide is designed so that the keys are unique, which is why we have the -1,
-2 there in the first place.

now this is all untested, written first thing in the day, so you might want
to try it out first...  but it should work.  now, when you want to look up a
set, you can do something like:

@set_info = @{$set_data{$set_id});

to retreive the array.  if you don't have the exact id, you can run a • "search":

@set_matches = grep /$partial_id/, keys %set_data;

does any of this make any sense to you?

Some.  Actually, I'm losing the ability to ask cogent questions.  In the line
above "@set_info = @($set_data($set_id));" for instance, I don't follow what
exactly is being assigned, but I'm not sure what to ask to get at the heart of
what I'm missing.

$set_data{$set_id} is reading from the hash we created earlier.  so it gets
a reference to an array.  to dereference it, we add a @{...} around it.
then, the array gets assigned to @set_info.

:)

Also, if I want to print the value of $i+1 in a loop based on incrementation
of $i, what do I do to represent that?

for ($i=0;$i<10; $i++) {
print "$i+1\n";
}

prints:

0+1
1+1
2+1

etc. and I want:

1
2
3


ahhh...

for ($i=0; $i<10; $i++) {
  print $i+1, "\n";
}

print accepts a list, so seperating parts with commas works.  another way
would be to do this (which is less readable, imo):

for ($i=0; $i<10; $i++) {
  print ($i+1). "\n";
}

and use the string concat operator (.), so that print gets a single value,
instead of a list.

Hope that helped!

Yeah!  Thank you _very_ much for all this.  I suppose I should be over at
com.lang.perl.newbie or something but this really is just me trying to
implement LShop improvements, not learn perl generally.

oh, I love preaching perl... so np :)

Dan



Message is in Reply To:
  Re: Access a LEGO set DB with your phone!
 
(...) The example at (URL) suggested using : for (@lines) {do stuff} But that didn't seem to work. I'm looking at a simple tutorial on foreach, but I don't get what the $set means or does for the loop. In your example, is it doing something like (...) (22 years ago, 18-Mar-02, to lugnet.off-topic.geek)

9 Messages in This Thread:



Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR