home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5484 < prev    next >
Encoding:
Text File  |  1992-08-25  |  1.5 KB  |  52 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!decwrl!sgi!wdl1!wdl39!mab
  3. From: mab@wdl39.wdl.loral.com (Mark A Biggar)
  4. Subject: Re: Fast String Operations?
  5. Message-ID: <1992Aug25.215959.549@wdl.loral.com>
  6. Sender: news@wdl.loral.com
  7. Organization: Loral Western Development Labs
  8. References: <1992Aug25.151625.3134@IDA.ORG>
  9. Date: Tue, 25 Aug 1992 21:59:59 GMT
  10. Lines: 40
  11.  
  12. In article <1992Aug25.151625.3134@IDA.ORG> rlg@IDA.ORG (Randy garrett) writes:
  13. >I'm looking for the fastest way to perform the following operation.
  14. >Basically, this is a conversion of one database format to another.
  15. >If I have a NULL field, indicated by 2 pipe symbols next to
  16. >each other, I want to insert either a -1 or a ~ between the 
  17. >two pipes, depending upon whether the type of that field
  18. >is a integer or a character.  I know the type of the field
  19. >because I've already pre-filled that array with the correct
  20. >types from the Data Dictionary (Thanks Sybase to Perl Interface!).
  21.  
  22. I probabily make more sense to use split for this, modifiy your code like so:
  23.  
  24. >#!/usr/local/bin/perl
  25. ># Walk thru a string; if find "||" insert either a ~ or a -1
  26. ># depending on value of @name 
  27. >
  28. >$string = "a|bcd||g||i|||";
  29. >@name = (1,2,1,1,2,1,2,1);
  30. >
  31. >$count = 0;  # index into @name
  32.  
  33. @string = split(/|/,$string);
  34. foreach $s (@string) {
  35.     $s = ($name[$count++] == 1 ? -1 : '~') if $s eq '';
  36. }
  37. $string = join('|', @string);
  38.  
  39. >print "$string\n";
  40.  
  41. The foreach could be written as a grep() but I think this was is more
  42. understandable.
  43.  
  44. --
  45. Perl's Maternal Uncle
  46. Mark Biggar
  47. mab@wdl1.wdl.loral.com
  48.  
  49.  
  50.  
  51.  
  52.