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

perl array print 3

Status
Not open for further replies.

dietmarp

Programmer
Dec 27, 2001
53
US
Hi,
I've run into following problem.

I need to process a list, and print every 5 errorcodes in one row and all errorcodes separated by a |. This is the list.
Code:
##############
#errorcode.lst
##############
errorcode=101
errorcode=222
errorcode=333
errorcode=444
errorcode=555
errorcode=666
errorcode=777
errorcode=888
errorcode=999
errorcode=000
errorcode=123
errorcode=234
errorcode=567
errorcode=890
errorcode=987
errorcode=342
errorcode=478

Thats the code I got so far.
Code:
#!/usr/bin/perl -w
use strict;
my $LOGFILE = "d:/logfile.txt";

#Get all errorcodes from errcode.lst and put them into an array
sub get_errcode {
  my $fh;
  my @errfile;
  my $ERRFILE = "d:/errorcode.lst";
  my @errorcode;
  open($fh, "<$ERRFILE") || &write_logfile($LOGFILE,"cannot read $ERRFILE");
	 @errfile=<$fh>;
  close $fh;
  foreach my $line (@errfile) {
	next if ($line =~ /^#/);
    chomp $line;	
	my($dummy, $errorcode) = split(/=/, $line); 
	push(@errorcode, $errorcode);
  }
	print @errorcode;
}
# Write Logfile
sub write_logfile {
my $message;
       ($LOGFILE,$message)=@_;
       open(LOG, ">> $LOGFILE") || die "can't open or append file $LOGFILE: $!";
       print LOG "$message";
       close LOG;
}

get_errcode();
Result running the code.

101222333444555666777888999000123234567890987342478


That's, how the result should look like.

101|222|333|444|555
666|777|888|999|000
123|234|567|890|987
342|478

Any help very much appreciated.
Dietmar
 
You'll need a `foreach' loop to process the array element-by-element. Additionally, a counter would be useful to keep track of which position in the array you're currently in. Basically, you want to print a newline ("\n") after every fifth array element, and a `|' after every other one.
 
Code:
my $cnt = 0;
foreach my $errorcode (@errorcode)
{
  $cnt++;
  if ($cnt == 5)
  {
     print "$errorcode\n";
     $cnt = 0;
  }
  else
  {
     print $errorcode;
  }
}
 
Code:
#!/usr/bin/perl
use strict;
use warnings;

my $i;

while (<DATA>) {
    print $1, (++$i % 5) ? "|" : "\n" if /errorcode=(\d+)$/;
}

__DATA__
##############
#errorcode.lst
##############
errorcode=101
errorcode=222
errorcode=333
errorcode=444
errorcode=555
errorcode=666
errorcode=777
errorcode=888
errorcode=999
errorcode=000
errorcode=123
errorcode=234
errorcode=567
errorcode=890
errorcode=987
errorcode=342
errorcode=478
seems to do what you want, without resorting to arrays.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for the responses.

Stevexxf, when I run your code it produces following output:

101|222|333|444|555
666|777|888|999|000
123|234|567|890|987
342|478|

How would I get rid of the last | .
I must not have lines ending with a | .
Rgds - Dietmar
 
Change to:
Code:
while (<DATA>) {
    print $1, ( ++$i % 5 and not eof ) ? "|" : "\n" if /errorcode=(\d+)$/;
}
 
Hi - thanks, that works. One more question.
Is it possible to put the lines generated into an array, so I can use them for further processing. I tried

Code:
while (<DATA>) {
push(@errorcode, $1, (++$i % 5 and not eof ) ? "|" : "\n" if /errorcode=(\d+)$/);
}

and receive following error:

syntax error at lala.pl line 10, near ""\n" if"
Execution of lala.pl aborted due to compilation errors.

Dietmar
 
Ups - to make it more clear, what result I'm looking for.
I need to be able to process the generated lines of numbers.
I thought, if I push the result to a new array, it would work. Here's the code.

Code:
#!/usr/bin/perl -w
use strict;
my $ERRFILE = "d:/errorcode.lst";
my $ERROR;
my $i;
my @list;
open(ERRORREAD, "<$ERRFILE") || &write_logfile(print,"- cannot read $ERRFILE");
while (<ERRORREAD>) {
	push @list,$1,(++$i % 5 and not eof ) ? "|" : "\n" if /errorcode=(\d+)$/;
}
close ERRORREAD;
print $list[0]."\n\n";
print @list;
####OUTPUT##########
101

101|222|333|444|555
666|777|888|999|000
123|234|567|890|987
####################
But if I print the first element of the array it is only the first number and not the line as I expected.

How would I need to change the code, that the
print $list[0]
would return 101|222|333|444|555
Rgds.
Dietmar
 
The comma is for the print statement to do multiple things at once, when you use it in push context, it will actually add more than one item to the array. You'll have to concatenate it.

Code:
while (<DATA>) {
    my $c = $1;
    $c .= ( ++$i % 5 and not eof ) ? "|" : "\n" if /errorcode=(\d+)$/;
    push (@list, $c);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top