Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Manipulating strings Message-ID: <1991May25.060211.3468@jpl-devvax.jpl.nasa.gov> Date: 25 May 91 06:02:11 GMT References: Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 68 In article setzer@matagh.ncsu.edu (Th'PoC) writes: : The problem: I have four files of identical length (< 5k). Each : contains 8 bit data, but do not contain any of `A'..`Z'. At any fixed : offset into each file, either 1) all four characters are identical, or : 2) one or more of the characters has the eighth bit set. I want to : generate an "overlay", i.e., if (1), output the character; if (2), : output a letter in `A'..`P' that will tell me which files had the : eighth bit set. : : The [*ugly*] solution: I `open'ed the four files, slurped each : of them into an array, and looped over the length of the array: : : -- : open(A, "< a") || die "can't open 'a'"; @a=split(//,join("",)); : # do the same for files "b", "c", and "d" : : for ($i=0; $i < $#a+1; $i++) { # do the `if's on `ord($a[$i])&0x80' etc. : -- : : The question: Is there a "prettier" way to do this? (There must be. : Isn't there a perl axiom that if you have to use an indexing variable, : you're doing it the "wrong" way?) It comes down to simple string : manipulation, but I can't see a way to use any of the implicit looping : constructs (`for (@a)', `foreach ...'). I would appreciate insight : into an alternate way to solve the problem. Thanks. Well, here's one way: #!/usr/bin/perl vec($foo,0,0); $a = `cat a`; $b = `cat b`; $c = `cat c`; $d = `cat d`; $string = $a; $a =~ tr/\200-\377/A/; $a =~ tr/A/\0/c; $b =~ tr/\200-\377/B/; $b =~ tr/B/\0/c; $c =~ tr/\200-\377/D/; $c =~ tr/D/\0/c; $d =~ tr/\200-\377/H/; $d =~ tr/H/\0/c; $highs |= $a; $highs |= $b; $highs |= $c; $highs |= $d; ($mask = $highs) =~ tr/A-Z/\177/c; $mask =~ tr/A-Z/\0/; $string &= $mask; $string |= $highs; print $string; : If the fundamentalists don't hate you, you have the wrong lifestyle. : -- James Nicoll If the fundamentalists hate you when they should hate your lifestyle, they've lost sight of the fundamental lifestyle. -- me Larry