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

trying to add a line number to my txt file array

Status
Not open for further replies.

madaxe

Technical User
Dec 21, 2004
44
US
CAN anybody tell me whats wrong with this code please...madaxe



#!/Perl/bin/perl

use CGI qw:)standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw:)flock :seek);
use strict;

my $z="0";
my $sitedata="main_data_base.cgi";


open(DAT,">>$sitedata") || die("Cannot Open File");
@raw_data=<DAT>;
close(DAT);

foreach my $i (sort pursesort @raw_data) {
chomp($i);
my ($compet,$purse,$golfer,$played) = split(/\|/,$i);

my $sitedata="main_data_base.cgi";
open(DAT,">>$sitedata") || die("Cannot Open File");
print DAT "$in{'$z'}\|$in{'compet'}\|$in{'purse'}\|$in{'golfer'}\|$in{'played'}\n";
close(DAT);

my $z='$z+1';

}
 
CAN anybody tell me whats wrong with this code please...
1. You initialize $z to the string "0". I think you want the number 0.
2. You open $sitedata for append with >>, then try to read from it.
3. Sorting routine pursesort is not shown, so can't comment on that.
4. Re-opening and closing $sitedata inside the for loop, on each iteration.
5. Referencing a hash called $in, which is not initialized anywhere that I can see.
6. Declaring a new variable called $z inside the for loop (this is not the same variable as the $z you declared earlier; it's confined to the block where it's declared) and setting it to the string "$z+1". I think you meant to increment your original $z instead of declaring a new variable.


 
Hi yes you are quite correct i ahve made some dreadful blunders thats what happens when your tired and cutting and pasting code from other code. I still have a problem my new code looks like this.

the error i get is this i dont understand? HELP

Global symbol "@raw_data" requires explicit package name at line 16 and 17

#!/Perl/bin/perl

use CGI qw:)standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw:)flock :seek);
use strict;


my $z=0;
my $sitedata="main_data_base.cgi";



open(DAT,">>$sitedata") || die("Cannot Open File");

@raw_data=<DAT>;
close(DAT);


foreach my $i (@raw_data) {
chomp($i);
my ($compet,$purse,$golfer,$played) = split(/\|/,$i);

my $sitedata="main_data_base.cgi";
open(DAT,"$sitedata") || die("Cannot Open File");
print DAT "{'$z'}\|{'compet'}\|{'purse'}\|{'golfer'}\|{'played'}\n";
close(DAT);


my $z=$z+1;


}
 
That's one I missed in my last post. You've got use strict at the top of the script (which is good). strict requires that you declare all your variables with my. @raw_data is not declared with my.

If you're trying to do what I think you're trying to do, it's simple. I think you're trying to:

1. Read a |-delimiited text file.
2. Sort on the second field ($purse).
3. Stick a line number on the front of each line.
4. Write it back onto the same file.

Is this correct?
Could you post a representative sample of your text file (preferably inside
Code:
 tags)?
 
hi yeah no probs here goes its at the bottom was not sure what u meant by [tags] so i pu them at the satrt and end of my line below

i also think i have got a little closer by not reopening the file each loop it now looks like this

Madaxe

#!/Perl/bin/perl

use CGI qw:)standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw:)flock :seek);
use strict;


my $z=0;
my $sitedata="main_data_base.cgi";


open(DAT,$sitedata) || die("Cannot Open File");

@raw_data=<DAT>;


foreach my $i (@raw_data) {
chomp($i);
my ($compet,$purse,$golfer,$played) = split(/\|/,$i);


print DAT "{'$z'}\|{'compet'}\|{'purse'}\|{'golfer'}\|{'played'}\n";
close(DAT);


my $z=$z+1;


}


[Andrew Allen|124.000|Couples, Fred|4]
[Bob Berry|235.000|Bohn, Jason|4]
[Charley Checker|110.000|Barber,Aaron|4]
[Dave Dean|110.000|May, Bob|4]
[Eric English|274.000|Lardon, Brad|4]
[Fred Flintstone|66.000|Na, Kevin|4]
[Gary Glitter|340.000|Palmer, Ryan|4]
[Harry Hover|115.00|Perks, Craig|4]
[Ilam Icem|150.200|Pohl, Dan|4]
[Jerry James|89.300|Peoples, David|4]

 
THANKS FOR YOUR HELP

im also trying to build a drop down menu from my array as well

madaxe
 
using the same txt file info i can get this to work but it will not print the new array to file i tried adding a > symbol in when i first open the file this has the effect of emptying my file, also i dont want to to print to screen the code is as follows and the presant output.

I dont understand why line 11 is the way it is in the output?

#!/Perl/bin/perl -wT


$sitedata="main_data_base.cgi";


open(DAT,"$sitedata") || die("Cannot Open File");

@raw_data=<DAT>;

print "Content-type: text/html\n\n";
print "<HTML><BODY>";


$z=-1;

foreach $array_data (@raw_data)
{


chop($array_data);
($compet,$purse,$golfer,$played) = split(/\|/,$array_data);


print DAT "$z\$compet\|$purse\|$golfer\|$played\n";

$z++;

print "$z\|$compet\|$purse\|$golfer\|$played";
print "<BR>\n";


}

close(DAT);

print "</BODY></HTML>";

OUTPUT

0|Andrew Allen|124.000|Couples, Fred|4
1|Bob Berry|235.000|Bohn, Jason|4
2|Charley Checker|110.000|Barber,Aaron|4
3|Dave Dean|110.000|May, Bob|4
4|Eric English|274.000|Lardon, Brad|4
5|Fred Flintstone|66.000|Na, Kevin|4
6|Gary Glitter|340.000|Palmer, Ryan|4
7|Harry Hover|115.00|Perks, Craig|4
8|Ilam Icem|150.200|Pohl, Dan|4
9|Jerry James|89.300|Peoples, David|4
10||||
 
open DAT ">myfile.text";
print DAT "ewgjhgjhgjhg myfi hgvh guf\n";
close FH;

hth
--paul

cigless ...
 
Here you go. I took out some of your header stuff and changed the input file name so I could test it. You can change it back.

I'm assuming you really want to overwrite your original file, rather than append to it, so I open the file with > to write to it. If you really want to append, change to >>.

If you click on "Process TGML" at the bottom of the box you type in when posting, you'll see what I meant by "code tags" (and much else besides).
Code:
#!perl
use strict;
use warnings;

my $sitedata = "madaxe.txt";
open(DAT, "<$sitedata") || die qq(Can't open "$sitedata" for read!\n);
my $z = 0;
my @data = map {$z++ . "|" . join("|", @$_)}
    sort {$a->[1] <=> $b->[1]}
    map {[split /\|/]} <DAT>;
close(DAT) || die qq(Can't close "$sitedata" after read!\n);
open(DAT, ">$sitedata") || die qq(Can't open "$sitedata" for write!\n);
print DAT @data;
close(DAT) || die qq(Can't close "$sitedata" after write!\n);
Output:
Code:
0|Fred Flintstone|66.000|Na, Kevin|4
1|Jerry James|89.300|Peoples, David|4
2|Charley Checker|110.000|Barber,Aaron|4
3|Dave Dean|110.000|May, Bob|4
4|Harry Hover|115.00|Perks, Craig|4
5|Andrew Allen|124.000|Couples, Fred|4
6|Ilam Icem|150.200|Pohl, Dan|4
7|Bob Berry|235.000|Bohn, Jason|4
8|Eric English|274.000|Lardon, Brad|4
9|Gary Glitter|340.000|Palmer, Ryan|4
HTH
 
hi this is what i get when i run it. It seems to be deleting the first line for some reason.is there any way to take the sort line out as well it causes problems as i get a not a numeric error when it tries to compare name agains name for the sort

thanks Madaxe

0|
1|Fred Flintstone|66.000|Na, Kevin|4
2|Jerry James|89.300|Peoples, David|4
3|Charley Checker|110.000|Barber,Aaron|4
4|Dave Dean|110.000|May, Bob|4
5|Harry Hover|115.00|Perks, Craig|4
6|Andrew Allen|124.000|Couples, Fred|4
7|Ilam Icem|150.200|Pohl, Dan|4
8|Bob Berry|235.000|Bohn, Jason|4
9|Eric English|274.000|Lardon, Brad|4
10|Gary Glitter|340.000|Palmer, Ryan|4
 
if i remove this line it wotk better

sort {$a->[1] <=> $b->[1]}

but i still get an error :-

Premature end of script headers: indexingscript2.cgi, referer: http://

also it still adds in an extra line at the end?

[0|Andrew Allen|124.000|Couples, Fred|4]
[1|Bob Berry|235.000|Bohn, Jason|4]
[2|Charley Checker|110.000|Barber,Aaron|4]
[3|Dave Dean|110.000|May, Bob|4]
[4|Eric English|274.000|Lardon, Brad|4]
[5|Fred Flintstone|66.000|Na, Kevin|4]
[6|Gary Glitter|340.000|Palmer, Ryan|4]
[7|Harry Hover|115.00|Perks, Craig|4]
[8|Ilam Icem|150.200|Pohl, Dan|4]
[9|Jerry James|89.300|Peoples, David|4]
[10|]
 
madaxe, you're getting the error because your input file contains a blank line (probably at the end) and the sort routine is trying to do a numeric comparison with a non-numeric value from this line. Here's a version of the program that will ignore blank lines in the input file. (Bolded lines have been changed/added from the original.)
Code:
#!perl
use strict;
use warnings;

my $sitedata = "madaxe.txt";
open(DAT, "<$sitedata") || die qq(Can't open "$sitedata" for read!\n);
my $z = 0;
[b]my @temp;
while (<DATA>) {
    next if /^\s*$/;
    push(@temp, [split /\|/]);
}
my @data = map {$z++ . "|" . join("|", @$_)}
    sort {$a->[1] <=> $b->[1]}
    @temp;[/b]
close(DAT) || die qq(Can't close "$sitedata" after read!\n);
open(DAT, ">$sitedata") || die qq(Can't open "$sitedata" for write!\n);
print DAT @data;
close(DAT) || die qq(Can't close "$sitedata" after write!\n);
That should take care of the problem.

 
Whoops, that should be
while (<DAT>) {
not
while (<DATA>) {


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top