home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5502 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  1.7 KB

  1. Path: sparky!uunet!mcsun!sunic!dkuug!uts!id.dth.dk!ej
  2. From: ej@id.dth.dk (Erik Johansen)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Fast String Operations?
  5. Message-ID: <1992Aug26.080304.29886@id.dth.dk>
  6. Date: 26 Aug 92 08:03:04 GMT
  7. References: <1992Aug25.151625.3134@IDA.ORG>
  8. Organization: Department of Computer Science
  9. Lines: 43
  10.  
  11. rlg@IDA.ORG (Randy garrett) writes:
  12.  
  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. >So, I read in a series of lines from a file.  If I find 2 pipe 
  23. >symbols adjacent to each other, "||", in the input string, I want
  24. >to insert either a ~  or a -1 depending on the type of that field,
  25. >which I get from the @name array.
  26.  
  27. Here is my rewriting of your code:
  28.  
  29. #!/usr/local/bin/perl
  30. # Walk thru a string; if find "||" insert either a ~ or a -1
  31. # depending on value of @name
  32.  
  33. $string = "a|bcd||g||i|||";
  34. @name = (1,2,1,1,2,1,2,1);
  35.  
  36. %defaults = (1, '-1', 2, "~");  # Specify defaults for each of your types
  37. @strings = split(/\|/, $string);
  38. for $count ( 0..8 )
  39.  {
  40.   $strings[$count] = $defaults{ $name[$count] } if $strings[$count] eq '';
  41.  }
  42. $string = join("|", @strings);
  43.  
  44. print "$string\n";
  45.  
  46.  
  47. - Erik
  48.  
  49.  
  50. -- 
  51. ---
  52. Erik Johansen                  E-mail: ej@id.dth.dk
  53. Institute for Computer Science, Copenhagen, Denmark
  54.