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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!noc.near.net!meiko.com!mike
  3. From: mike@meiko.com (Mike Stok)
  4. Subject: Re: Fast String Operations?
  5. Message-ID: <1992Aug26.113147.10966@meiko.com>
  6. Sender: news@meiko.com
  7. Organization: Meiko Scientific Corp.
  8. References: <1992Aug25.151625.3134@IDA.ORG>
  9. Date: Wed, 26 Aug 1992 11:31:47 GMT
  10. Lines: 60
  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. >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's the code I'm using.  It works, but the bad news is that
  28. >I process 800+ Megabytes of data at a crack with this. It 
  29. >takes almost a day of CPU time on a Sparc 2!  Any words of
  30. >wisdom on how to speed it up would be greatly appreciated.
  31.  
  32. My guess at the code would be something like:
  33.  
  34. #!/usr/bin/perl
  35.  
  36. $string = "a|bcd||g||i|||";
  37. @name = (1,2,1,1,2,1,2,1);
  38.  
  39. if ($string =~ /\|\|/)
  40. {
  41.   chop ($string);
  42.   @field = split (/\|/, $string, @name);
  43.   for ($index = 0; $index < @field; $index++)
  44.   {
  45.     $field[$index] = ('', '-1', '~')[$name[$index]] if $field[$index] eq '';
  46.   }
  47.   $string = join ('|', @field) . '|';
  48. }
  49.  
  50. print "$string\n";
  51.  
  52. This would only do any processing if there were 2 adjacent |s in the
  53. string, and I assume that the | is used as a field terminator rather
  54. than a separator (hence the chop and the . '|' after the join), and
  55. that there will always be the right number of fields in a string, and
  56. that $[ is left at the default value of 0.  It produces a different result
  57. to your code :-(, but if field 0 is the "a", 1 is "bcd", and field 2
  58. is the first empty field, I see that $name[2] == 1, so this is an empty
  59. integer field so it becomes '-1'.  My result $string was a|bcd|-1|g|~|i|~|-1|
  60. but yours was a|bcd|~|g|-1|i|-1|~|...
  61.  
  62. I haven't measured how fast it goes compared to your code, but I would
  63. happily buy you a beer if it's slower :-)  (I guess I should owe you one
  64. anyway as I get a different answer)
  65.  
  66. Mike
  67. --
  68. The "usual disclaimers" apply.    | ... many were weak and confused, succumbing
  69. Mike Stok                         | to drink or drugs whenever possible ...
  70. mike@meiko.com                    |
  71. Meiko tel: (617) 890 7676         |                          Hunter S. Thompson
  72.