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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

line not equal to blank or nothing but space chars 1

Status
Not open for further replies.

dingleberry

Programmer
Dec 13, 2002
143
US
I'm trying to figure out the syntax to test a line of a file I am parsing to determine if it is blank or not. Basically my file looks like this.

Joe
address 1
joe@testemail.com

fred
address 2
fred@testemail.com

etc
etc
etc

and what I'm doing is attempting to assign the values between the blank lines to an array. So when the script finds the login name (ie joe or fred) it starts populating the array until a blank line or line with nothing but spaces occurs. Here is my snippit.

open CUSTOMER, $customer or die "Can't open $log_file for reading: $!\n";
foreach my $line (<CUSTOMER>) {
chomp $line;
if ($line eq &quot;$login&quot;) {
while ($line != (/^\s*$/)) {
push (my @lines, $line);
}
print @lines;
}
}

I think the problem is in the while loop syntax, but I've never tested a line to see if it's blank this way so it's probably something silly like using ne instead of != or !=~ instead of !=.

I'm stumped. I think I've tried all these options. Any idea?

Thanks,
Dan
 
Hi,

Try this, this is what I use and it seems to work

Code:
while($line eq '') {
    # do somthing with the blank line
   }else
   {
# do somthing with the non-blank line

}

Kind Regards, Paul Benn

**** Never Giveup, keep trying, the answer is out there!!! ****
 
In logic, that does make sense however I will try to explain why it doesn't work (at least in my very limited knowledge.)

I'm running a foreach loop on this file where I'm checking
if ($line eq $login) {..

If directly underneath that command I put a while ($line eq '') {...

we terminally loop because $line can only be one or the other I think. If there was a way to just have the program start to fill a seperate array until a blank line was reached from the point where $line = $login, I could then reference it for later use.

here is where the code is now, but still not working.


my $line;
open CUSTOMER, $customer or die &quot;Can't open $log_file for reading: $!\n&quot;;
while (<CUSTOMER>) {
foreach my $line (<CUSTOMER>) {
chomp $line;
if ($line eq $login) {
push (@lines, $line); #here is where I would like to put in some command that t
ells the program to push @lines with every line until a blank line is reached.
}
}
}

 
regarding your first post:

the &quot;doesn't match&quot; operator (opposite of the &quot;match&quot; operator, '[tt]=~[/tt]') is '[tt]!~[/tt]', not '[tt]!=[/tt]', which is the numeric 'not equals' operator.

Here is an example of searching through for one unique login name.
Code:
while (<CUSTOMER>) {
    # loop until $login name is found
    next unless /^$login$/;
    # populate array until a blank line is reached
    until (/^\s*$/) {
        chomp;
        push @lines, $_;
        # read in next line
        $_ = <CUSTOMER>;
    }
    print join(', ', @lines),&quot;\n&quot;;
    # since login name is unique quit searching through file.
    last;
}
jaa
 
jaa,

That was EXACTLY what I was looking for. I'm very very grateful and humbled that you would take time to show me that.

Thank you very much,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top