Stephen Kelly wrote:
> 
> hi there

Hello,

> i'm trying to split a string from a line of text in a file - the split
> delimiter is a tab character '\t' - i then need to compare the 2 bits on
> either side of the tab to see if they are equal - if not eq - i write to
> tidy else i write to mess
> 
> ? confused - this bit of code is giving me empty files ?

You haven't removed the end of line character(s) so the two fields will
never be equal.

use warnings;
use strict;

> while (<INPUT>)
> {

You need to chomp() here:

chomp;

>  next if ($_=~/^\#/); #if line begins with a #
>  next if ($_=~/^\s*(\#|$)/); #if line is blank

The first line is not needed as the second line does the same thing.

next if /^\s*(?:#|$)/;

>  if ( $_ =~/\w/)
>  {print MESS "$_"}

The quotes nor the use of $_ are requied.

print MESS if /\w/;

>  if ( $_ !~/[A-Za-z]/)
>      {print MESS "$_"}
>  else {print TIDY "$_"}
> 
>  # split the string into 2 parts string 1, 2 separated with a tab.
> 
>  @_= split(/\t/,$_);
> 
>  # compare the first element of the list with the last element.
> 
>  if (@_[0] eq @_[1])

You are using array slices when you should be using scalars.

if ( $_[0] eq $_[1] )

> {print MESS "$_"}
>  else { print TIDY "$_"}
> }

A more perl-ish way to write that:

while ( <INPUT> ) {
    next if /^\s*(?:#|$)/;   #if line begins with a # or line is blank
    print MESS if /\w/;
    print { /[A-Za-z]/ ? *TIDY : *MESS } $_;

    # split the string into 2 parts string 1, 2 separated with a tab.
    my ( $first, $second ) = split /\t/;

    # compare the first element of the list with the last element.
    print { $first eq $second ? *MESS : *TIDY } $_;
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to