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

Perl Loop Control - Print header once 1

Status
Not open for further replies.

Extension

Programmer
Joined
Nov 3, 2004
Messages
311
Location
CA

Hi,

I'm trying to control a loop so that the header is only printed once within the loop. Obviously, with my current code, the header will inherent the loop propertie; it will get printed
multiple times if there is more than one record.

Should I use labels and use the "next" function ?

Any help or guidance would be appreciated.
Thanks

Code:
open (FILE, "File.txt") or die("Error");
			
		while ( my $records = <FILE>) {
   			chomp $records;
			
			my @field=split(/\|/,$records);

			$Value0 = $field[0];
			$Value1 = $field[1];
						
			# Print header once if at least one record exists
			if ($Value1 ne '') {	
				print qq(
					------------------------- \n
					| VALUE ID | VALUE NAME | \n
				);	
			}	
			
			# Print not Null records
			if ($Value1 ne '') {	
				print qq(
					| $Value0 | $Value1 | \n
				);	
			}		 
			
		}

close FILE;
 
Code:
open (FILE, "File.txt") or die("Error");
        my $i = 0;
        while ( my $records = <FILE>) {
               chomp $records;
            
            my @field=split(/\|/,$records);

            $Value0 = $field[0];
            $Value1 = $field[1];
                        
            # Print header once if at least one record exists
            if ($Value1 ne '' && $i == 0) { 
                print qq(
                    ------------------------- \n
                    | VALUE ID | VALUE NAME | \n
                );
            }    
            
            # Print not Null records
            if ($Value1 ne '') {    
                print qq(
                    | $Value0 | $Value1 | \n
                );    
            }        
            $i++;
        }

close FILE;

M. Brooks
 
That was the easy way though. I suggest saving some lines by writing it this way though.
Code:
open FILE, "File.txt" or die $!;
          
my $i = 0;  
while ( <FILE> ) {
    chomp;
       
    my ( $id, $name ) = split( /\|/, $_ );
                        
    # Print header once if at least one record exists
    if ( $name ne '' && $i == 0 ) {    
        print qq{
            -------------------------
            | VALUE ID | VALUE NAME |
        };    
    } 
            
    # Print not Null records
    if ( $name ne '' ) {    
        print qq{
            | $id      | $name      |
        };    
    }        
    $i++;      
}

close FILE;

M. Brooks
 
why not just print the header before the loop?
 
You could be correct but I would still just print the header before the loop. But you can use the input record line number variable ($.) to see what line of the file we are on:


Code:
my $header = qq~
            -------------------------
            | VALUE ID | VALUE NAME |
            -------------------------~; 
open FILE, "File.txt" or die $!;
while ( <FILE> ) {
    print $header if [b]$. == 1[/b]; 
    chomp;
    my ($id,$name) = split(/\|/);
        print qq~
            | $id     | $name       |
        ~ if $name;    
}
close FILE;

which I am assuming means there will be at least one record. If line one has no record and is the EOF you have to check for that condition.
 
just want to say there is nothing wrong with your suggestions mbrooks, they will work fine. I should have said that previously. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top