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!

Parsing Data Using Split

Status
Not open for further replies.

trumb1mj

MIS
Joined
Jun 29, 2006
Messages
12
Location
US
Task Overview: I am writing a Perl script to look through 'input.txt' and put this data into 'output.txt".

The input.txt file looks like this:

ID=10; Strike=233; etc...

I want my output.txt file to look like this (or similar):

10,233, etc...

I have my out put looking like this, the only problem is, that I want to be able to add multiple lines to the output.txt file by adding to the ID column without chaging the input.txt file.

An example of this is:

10,233
11,233
12,233
etc...

Can anyone help me with this? He is my code:

#!/usr/bin/perl


open(FILE,"input.txt") or die("Unable to open file");
@data=<FILE>;
close(FILE);

$line = @data[0];

@splitline = split /;/, $line;

#@splitlineb = split /=/, @splitline;

foreach $piece(@splitline)
{
@fields = split /=/, $piece;
#print @fields[0];
#print "\n";
print @fields[1];
print "\n";
}
 
Have to ask what input.txt looks like, and what you want output.txt to look like...

input.txt:

ID=10; Strike=233; ID=11; Strike=233; ID=12; Strike=233; ID=10; Strike=234;
..or..
ID=10; Strike=233;
ID=11; Strike=233;
ID=12; Strike=233;
ID=10; Strike=234;

output.txt:

10,233
11,233
12,233
10,234
..or..
10,233,234
11,233
12,234
..or..
10,233,11,233,12,233,10,234
??
 
the input will be a single line:

ID=10; Strike=233;

and the ouput will be several lines from that single line (only changing the the ID):

10,233
11,233
12,233
 
#!/usr/bin/perl
open(FILE,"input.txt") or die("Unable to open file");
$line = <FILE>;
close(FILE);

print "$1,$2\n" while( $line =~ /ID=([^;]*); Strike=([^;]*)/g );
 
Well that would work, the only problem is, I have taken my headings out of the input.txt file. I have made some changes since the last update. Here there are:

#!/usr/bin/perl


open(FILE,"input.txt") or die("Unable to open file");
@inputFileData=<FILE>;
close(FILE);
$line=@inputFileData[0];


print "Number of test values?";
$times = <STDIN>;
#@multiply = ($times);

@splitline = split /;/, $line;

$i=0;
foreach $piece(@splitline)
{
@fields = split /=/, $piece;
@dataStore[$i]=@fields[1];


$i++;
}

for ($i=0; $i<$times; $i++)
{
foreach $data(@dataStore)
{

print $data;
print ",";
}
print "\n";
}

The last foreach statement is where I would like to throw some sort of statement to count up a field (not necessarily the 'ID' field, jut the frst field). Can you help me with this?
 
As for reading the ID/Strike values from the input file, you're saying that the names may be different, but they're still to be treated the same ?

Code:
print "$1,$2\n" while( $line =~ /=([^;]*);.+?=([^;]*)/g );

What's the point of the $times variable ?

What are you looking to do in 'counting up a field' ?
..Count the number of times that the first field was a particular value (10, 11...etc) ?

..or sum the value of the second field for each value of the first field ?
 
I'm sorry about the confusion, I am very new to Perl. Maybe it would help by discussing the problem. I am a QA analyst and I am trying to test some software. I am trying to easily create test data for an SQL db, by using a template(input.txt) file. I want my Perl script to be runnable from the command line and this is the significance of the 'Times' variable. I have change my script again as follows:

#!/usr/bin/perl

open(FILE,@ARGV[0]) or die("Unable to open file");
@inputFileData=<FILE>;
close(FILE);
$line=@inputFileData[0];

$times = @ARGV[1];

@splitline = split /;/, $line;

$i=0;
foreach $piece(@splitline)
{
@fields = split /=/, $piece;
@dataStore[$i]=@fields[1];

$i++;
}


for ($i=0; $i<$times; $i++)
{
foreach $data(@dataStore)
{
print $data;
print ",";
}
print "\n";
}

This time the I have made the script so the user can specify how many records they wish to create by typing './conv input.txt 5 > output.txt' FYI:'conv' is the script name and in this case the output created by 'conv' is sent to 'output.txt'

Everything is working with my script except I still haven't figured out a way to count the first field for each new record that is added to the output (or printed).

does this help?
 
Do you mean replacing the output for( foreach() ) construct with ...

Code:
for( my $i=0; $i<$times; $i++ ) {
  print "$dataStore[$i*2],$dataStore[$i*2+1]\n";
}

??

 
this is the area of focus, but I did not get the correct output.... it only changed the format and did not add 1 to the first column. any other ideas? thank you so much for trying.
 
OK, given this input:
Code:
ID=1; Value=100; ID=2; Value=200; ID=2; Value=400; Num=3; thing=800; Num=4; thing=1600; XX=5; Word=alpha; XX=6; Word=beta

and a value of 4 for $times,

what exact output would you be wanting ?
 
I would want:

1,100,200,2,400,3,800,4,1600,5,alpha,6,beta;
2,100,200,2,400,3,800,4,1600,5,alpha,6,beta;
3,100,200,2,400,3,800,4,1600,5,alpha,6,beta;
4,100,200,2,400,3,800,4,1600,5,alpha,6,beta;
5,100,200,2,400,3,800,4,1600,5,alpha,6,beta;

and so on. so the input is used to create several outputs.
 
1) Where does a value of 4 for $times come into that output?

2) Are you missing any fields in that output ?? You have 100 directly followed by 200. I'm assuming you missed out a '2'.

3) You are losing the very first name/value pair and replacing it with an loop iteration value. Should there only have been 4 lines (value of $times) ??

With that in mind:
Code:
#!/usr/bin/perl
$times = 4;
$line = <DATA>;
chomp($line);
push @datastore, $1, $2 while( $line =~ /=([^;]*); .+?=([^;]*)/g );
for(1..$times) {
  print;
  print ",$datastore[$_]" for(1..@datastore-1);
  print ";\n";
}
__DATA__
ID=1; Value=100; ID=2; Value=200; ID=2; Value=400; Num=3; thing=800; Num=4; thing=1600; XX=5; Word=alpha; XX=6; Word=beta



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top