in reply to using join with a print ref statement

Hmmmm ... i don't like the idea of determining whether or not search results were found by using ref. If i were designing this i would create a subroutine that either returned the results in an array (or array ref) if results were found or returned undef if no results were found. This is a much simpler approach that will most likely be more robust in the long run, i'll wager. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
  • Comment on (jeffa) Re: using join with a print ref statement

Replies are listed 'Best First'.
Re: (jeffa) Re: using join with a print ref statement
by Bismark (Scribe) on Jan 20, 2003 at 17:58 UTC
    Actually, I do use a sub to return the searchresults, kind of like a hash, then I slice it. at least I think that is the terminology. The actual code is in my scratchpad. Its the best I could do with a lot of help from this site and the 3 weeks that I have been using Perl. I hope to make it a lot better as I go along. Kerry
    "Yet what are all such gaieties to me
    Whose thoughts are full of indices and surds?"
    quotes the Lama
      I modified the code on your scratchpad (here is the original for hysterical raisins): Into the following (untested) code:
      my $searchresult = search( part => $part, rev => $rev, filename => './file.db', ); print $searchresult ? "Located Part Number: @{[join ':', @$searchresult ]}\n" : "Your Part Number ($part) Rev ($rev) could not be found....\n" ; sub search { my %args = @_; my $found = 0; my @record; open PARTS_DB, $args{filename} or die "$args{filename}: $!"; while (!$found and my $record = <PARTS_DB>) { chomp $record; @record = split /\|/, $record; if ($record[PART] eq $args{part} && $record[REV] eq $args{rev}){ $found = 1; last; } } close PARTS_DB; return $found ? \@record : undef; }
      This should have the same results as your version, but i think my version is a bit more clear on the intent.

      UPDATE:
      On another note, if the array is not going to be considerably large, then consider returning the array a list instead of a reference to the array:

      sub search { ... yadda yadda yadda ... return @record; } my @searchresult = search(... yadda yadda ...); print @searchresult ? "got it: @searchresult\n" : "nadda\n";
      much simpler, but not very good if @record is going to be large.

      UPDATE x 2
      Sorry about that, change the && to and on line 38 (i told you this was untested ;)). Actually, you could remove the check for !$found all-together (untested and unplanned! ;)):
      while (my $record = <DATA>) {

      or you could remove the last statement (not the last statement ;)). The other errors are cut and paste errors on your behalf due to code wrapping on this site. Try it now. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        I tried to test this code and received quite a few error messages. I do not know enough yet to sort them all out. Here they are so you can take a shot at it. The record returned will eventually be quite large so I am trying to learn what I can to get it all into a database. Just do not know how, yet. Error messages:
        Can't modify logical and (&&) in scalar assignment line 38 near "<PART +S_DB>) " syntax error at line 42, near "+}" syntax error at line 46, near "}" You aren't allowed to assign to the item indicated, or otherwise try t +o change it.
        Kerry
        "Yet what are all such gaieties to me
        Whose thoughts are full of indices and surds?"
        quotes the Lama