Solved

Perl Output a file

Posted on 2014-12-10
2
287 Views
Last Modified: 2014-12-10
Hi,

I wrote a perl script that does the following things.

1) Inputfile is provided
2) split input file using \n
3) if a line starts with "EMP" string then split the line with "|" and get the first word which is EMP ID
4) there might be multiple lines that starts with EMP so get all EMP ID's and out put those to a txt file

I wrote the below script but this one is not working not sure where did I made the mistake.

Sample Input file:

05|AAAAA|BBBBBBBB|BBBBCVCCBVBCV
EMP|10000000001|00000078|ENG|1
300|03/03/2014|12/31/9999|0.00
05|ASDFASDFAAAA|BBASDFASDBBBBBB|BBASDFASDBBCVCCBVBCV
EMP|100000123134|000000723128|ENG|1
300|01/12/2014|11/3/9999|0.00
TESTSCRIPT.txt
0
Comment
Question by:shragi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40492595
This should mostly fix your script.  The problem is your handling of the file.  "while (<X>)" is already reading the file line by line.

With the "for 2.." are you trying to skip the first line of the input file, the first two lines, or something else?
Also see the comments in the script about your output lines - they almost certainly aren't doing what you want.
#!perl

use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;

my $helpme = 0;
my $man = 0;

my $inputFileName = 'C:\\Testfile.txt';
my $outputFileName = 'C:\\Testfile_Amt.txt';

my $errorcode = 0;
my $DEBUG=0;

if(exists $ENV{DEBUG}) {
	$DEBUG = ($ENV{DEBUG} eq "") ? 0 : $ENV{DEBUG};
}

GetOptions('help' => \$helpme, 'man' => \$man, 'infile=s' => \$inputFileName, 'outfile=s' => \$outputFileName) or pod2usage(2);

pod2usage(1) if $helpme;
pod2usage(-verbose => 2) if $man;

die 'No input file name specified!' unless $inputFileName;
die 'No output file name specified!' unless $outputFileName;

open(INFILE, '<', $inputFileName) or die "Could not open input file: $!";
open(OUTFILE, '>', $outputFileName) or die "Could not open/create output file: $!";

while(<INFILE>) {
	chomp;
	### possible skip of first line or two
	if (/^EMP\|([^|]+)\|/) {
		my $empID = $1;
		### what are you trying to do with these lines?
		### they almost certainly don't do what you want
	    #print OUTFILE "I " . "0" x ($empID). "\n";
   	    #push(@employee, $empID);
		print OUTFILE "I ", $empID, "\n";
	}
}

close INFILE;
close OUTFILE;

exit $errorcode;

Open in new window

0
 

Author Comment

by:shragi
ID: 40492612
Thanks Wilcoxon :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question