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 / 3602
3601  |  3603
Subject: 
Re: Access a LEGO set DB with your phone!
Newsgroups: 
lugnet.off-topic.geek
Date: 
Mon, 18 Mar 2002 17:31:25 GMT
Viewed: 
307 times
  
In lugnet.off-topic.geek, Dan Boger writes:
In lugnet.off-topic.geek, Christopher L. Weeks writes:

while ($i > 0) {
$set = $sets[$i];  #to assign each array item temporarily

first of all, you're running an open ended loop on the array, when you can
just run through each element:

foreach $set (@sets) {

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?

@row = split (/\t/, $set)];  #to split each row into columns

that trailing ']' is a typo, right?  I'm not running this, but I think that
would be a syntax error...

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?

$rowref = \@row  #attempting to create a reference scalar to rows
rows[$i] = $rowref;  #load the array of references
print"\ncheckpoint=2.$i\n$rows[$i]";  #trying to debug

I'm guessing that all these checkpoints show the same array was referenced?
since it's the same @row that you keep assigning values to, you get the same
reference all the time...

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?

$i++;
if (@sets[$i] == EOF || $i > 10000) {$i = -1};
}  #what's a better way to terminate the loop?

not use a counter, but loop with a foreach, or with a for loop:

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

How does $#sets evaluate?

ok, there are
two basic ways of doing this.  one, save each row in the list in a seperate
element (probably in a hash for fast lookup), and split it when you retrieve
the data...  option 2, split the data beforehand (good if you want to save
on cpu), and stored the resulting array in a hash (which is kinda what
you're trying to do).  so here's how I'd do it:

my %set_data = ();  # empty hash

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

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?

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?

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.

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?

I'm not sure what you want to do, but it sounds like something like this?

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

???  can't be.

not quite.

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

prints:

0+1
1+1
2+1

etc. and I want:

1
2
3

I know I could :

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

But I'm pretty sure there must be a way to operate within the print statement.

you could use perl for that too,

I actually meant, "what should I be putting between the slashes to break it • up
by field."  But after writing that, I discovered that \t means tab and that
works perfectly.  I'd been trying stuff like /  */ to represent more than one
space and stuff, but nothing worked for the whole data source.

well, tabs are not spaces.  you could have done something like /\s\s*/
(where \s matches any whitespace, see man perlre), or just /\s+/ (since '+'
means at least one of the preceding expression)...

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.

Chris



Message has 1 Reply:
  Re: Access a LEGO set DB with your phone!
 
(...) 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. (...) maybe I'm misunderstanding again... @array = split /\t/, (...) (23 years ago, 18-Mar-02, to lugnet.off-topic.geek)

Message is in Reply To:
  Re: Access a LEGO set DB with your phone!
 
(...) first of all, you're running an open ended loop on the array, when you can just run through each element: foreach $set (@sets) { (...) that trailing ']' is a typo, right? I'm not running this, but I think that would be a syntax error... (...) (...) (23 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

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

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