Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lower Case lines

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
0
0
US
I want to loop thru a file and lower case everything EXCEPT for test within double quotes...I just want to leave that text alone. Is there a simple way to do this? I can easily write something that will work, but it feels more like a brute force method than what I want. For example, I could go line-by-line, character by character, and check "is this character within quotes?". If not, lower case it. Is there a better way of doing it?

Thanks in advance!
 
well, you will have to check for quotes, how you do it depends on how much you can truct the data. Are the quotes always symetrical? What happens if they are not, what about embedded quotes? Post some sample data.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Really, it just comes down to this:

Original:

A = "Hello World"
B = C

Becomes this:

a = "Hello World"
b = c

What I'm working with doesn't get much more complex than that.
 
one possibility:

Code:
$foo = 'blah';
if ($foo !~ /"/) {
   $foo = lc $foo;
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The only way I can see to do that is by splitting on double quotes:
Code:
while(<DATA>){
  my$within=0;
  my@parts=split/(")/;
  for(@parts){
    if($_ eq'"'){
      $within=!$within;
    }else{
      $_=lc$_ unless$within;
    }
  }
  print join'',@parts;
}
__DATA__
A = "Hello World"
B = C

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Yes, that "split" way was kind of what I was just thinking about. However, the quotes aren't included in the split results...are they? Anyway, I was thinking that I could split on '"', then just lowercase every other element (0,2,4,etc...)? Does that sound about right?
Code (not tested):
Code:
while (<DATA>){
  my $count = 0;
  my @parts = split/"/;
  for (@parts){
    $parts[$count] = lc($parts[$count]) if (!($count % 2));
    $count++;
  }
  print join '', @parts;
}
__DATA__
A = "Hello World"
B = C
 
Here's what I think I've ended up with (until I find some problem ;-) :
Code:
while (<DATA>) {
  my $count = 0;
  my @parts = split /"/;
  for (@parts) {
    if ( !( $count % 2 ) ) {
      $parts[$count] = lc( $parts[$count] )
    } else {
      $parts[$count] = "\"$parts[$count]\"";
    }
  $count++;
  }
  print join '', @parts;
}
__DATA__
A = "Hello World"
B = C
 
Double quotes in my code are included in the split because of the parentheses: that code works with multiple texts, adjacent or not, within quotes, and also when the line starts with a quote.
Your code shouldn't work if the quoted text is at the beginning and with multiple adjacent quotes.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Agreed. Fortunately, in the files I'm working with, none of the quotes are at the beginning of the line nor adjacent to each other.

Thanks for the feedback!
 
did you try my suggestion? If your data is really that simple it will work.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

I looked at your suggestion...HOWEVER, it fails on something like:

A = "Hello World"

Your suggestion will skip that entire line...I need the result as:

a = "Hello World"

 
ah, I misunderstood your previous examples.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I am not sure this is any better than other suggestions, but here is a way not using split:

Code:
while (<DATA>){
   if (s/^([^"]+)(".*")$/lc($1).$2/eg) {}
   else {
      $_ = lc $_;
   }
	print;
}	
__DATA__ 
A = "Hello World"
B = C

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top