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 / 3601
3600  |  3602
Subject: 
Re: Access a LEGO set DB with your phone!
Newsgroups: 
lugnet.off-topic.geek
Date: 
Mon, 18 Mar 2002 13:43:05 GMT
Viewed: 
249 times
  
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) {

@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...

$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...

$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++) {....}

I'm obviously missing at least one critical issue.

Ideally, you'd explain what I'm doing wrong.  If you're going to supply an
alternate script, please don't make it a single line.  Perl junkies who love
those little indecipherable things aren't really helpful to me :-)

yah, they're fun to play with, but not a great learning tool.  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
foreach $set (@sets) {
  my $array_ref = [];  # a reference to an annonymous array - a new one created
                       # in each iteration of the foreach
  @$array_ref = split /\t/, $set;
  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
}

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?

Since I discovered \t as a split() criterion I've been thinking of just loading
the whole thing as a single array and then just searching for the parts as
needed.  Is that a better way to do this?

that's the first option I mentioned above... save each line as a scalar for
a set, and parse it when you need the data.  That's actually how peeron
looks up the data for the inventory system, since I wasn't too worried about
cpu usage, and didn't want to mess with MLDBM files (to store persistant
hashes of arrays).

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 was trying to figure that out yesterday,
but now that I'm thinking it about it freshly, it seems probable that I just
have to enclose it in backticks.  I don't actually know the rules for these
things, I'm just picking it up bit by bit.

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.

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!

:)

Dan



Message has 1 Reply:
  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)

Message is in Reply To:
  Re: Access a LEGO set DB with your phone!
 
(...) Well, I've discarded the first part...the explanatory text up through the big ________. Then I've read the rest of the file into an array where each item in the array is one line. So now I'm trying to split() the @array[items] by column, (...) (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

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