Home: Perl Programming Help: Beginner:
importing txt file to array problem



gerble1000
User

Oct 11, 2012, 2:20 AM


Views: 5175
importing txt file to array problem

ok i have a code wich will import a txt file into an array as follows

Code
open(FILE,'details.txt'); 
my @details = <FILE>;
close(FILE);


now this one works fine and i can use the lines in the array as follows $details[0] and so on

my problem is when i try and make 2 arrays separate like this

Code
open(FILE,'fbg.txt'); 
my @lines = <FILE>;
close(FILE);

open(FILE,'details.txt');
my @details = <FILE>;
close(FILE);


for some strange reason it will not work on the fbg.txt
there is nothing strange about the txt file itself.

could somebody explain what i am doing wrong please.
i get no error message and there is nothing in the new array but the details array still works fine.
i must be using the wrong syntax or something


BillKSmith
Veteran

Oct 11, 2012, 5:38 AM


Views: 5163
Re: [gerble1000] importing txt file to array problem

Always use strict and warnings.
Always use the three argument form of open.
Always use lexical file handles.
Always test the status of open.

Perl will probably tell you what is wrong.


Code
use strict; 
use warnings;
open(my $FILE, '<', 'fbg.txt') or die "unable to open 'fbg.txt': \$!";
my @lines = <$FILE>;
close($FILE);

open($FILE, '<', 'details.txt') or die "unable to open 'details.txt':\!";
my @details = <$FILE>;
close($FILE);
Good Luck,
Bill


gerble1000
User

Oct 11, 2012, 6:47 AM


Views: 5160
Re: [BillKSmith] importing txt file to array problem

thanx for that but i realised that the arrays were working but it had carrage returns on the end. i have found a code that does remove the carrage returns but i am unsure how to do a foreach loop

here is what i have for the for loop


Code
foreach (@details){ 
$details[$_] =~ s/\r|\n//g;
}


what am i doing wrong here


(This post was edited by gerble1000 on Oct 11, 2012, 7:44 AM)


FishMonger
Veteran / Moderator

Oct 11, 2012, 8:05 AM


Views: 5157
Re: [gerble1000] importing txt file to array problem


Code
chomp @details;


Please read the documentation for the chomp function.

perldoc -f chomp


(This post was edited by FishMonger on Oct 11, 2012, 8:06 AM)


gerble1000
User

Oct 11, 2012, 8:30 AM


Views: 5153
Re: [FishMonger] importing txt file to array problem

i could only find this example
this uses a while loop instead of foreach.
when i ran my foreach code the server crashed. hence why i am asking on here :)


Code
     while (<>) { 
chomp; # avoid \n on last field
@array = split(/:/);
# ...
}



FishMonger
Veteran / Moderator

Oct 11, 2012, 8:42 AM


Views: 5149
Re: [gerble1000] importing txt file to array problem

The foreach loop you posted would not crash the server, so I can only assume that the actual foreach loop that you're executing is doing something different from what you posted. We can't help you troubleshoot code that you haven't shown us.

We also need details on what you mean when you say the server crashed.

BTW, it's almost always better to use a while loop instead of slurping the file into an array and then loop over that array.


gerble1000
User

Oct 11, 2012, 8:46 AM


Views: 5146
Re: [FishMonger] importing txt file to array problem

this is my attempt that crashed the server.
it must have made my cpu go through the roof as i had to hardreset it

Code
for my $i (@details) {  
$details2[$i] =~ s/\r|\n//g;
}



FishMonger
Veteran / Moderator

Oct 11, 2012, 8:49 AM


Views: 5143
Re: [gerble1000] importing txt file to array problem

In what way did it "crash the server"?

It will generate a warning, but will not crash the server.


gerble1000
User

Oct 11, 2012, 8:52 AM


Views: 5142
Re: [FishMonger] importing txt file to array problem

it was as if the program was in an infinate loop


BillKSmith
Veteran

Oct 11, 2012, 9:40 AM


Views: 5135
Re: [gerble1000] importing txt file to array problem

Refer: perldoc perlsyn for 'for' and 'foreach' loops. The loop variable is not an index.

Quote
If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop.


Code
for $i (@details) {  
$i =~ s/[\r\n]//g;
}

Good Luck,
Bill


gerble1000
User

Oct 11, 2012, 10:05 AM


Views: 5134
Re: [FishMonger] importing txt file to array problem

i have managed to it with a for loop.


Code
open(FILE,'fbg.txt'); 
my @details2 = <FILE>;
close(FILE);
my $arraySize = scalar (@details2);
for (my $i = 0; $i < $arraySize; $i++)
{
$details2[$i] =~ s/\r|\n//g;
}


this works perfectly. how would chomp do this better


gerble1000
User

Oct 11, 2012, 10:28 AM


Views: 5131
Re: [BillKSmith] importing txt file to array problem

i was close then :)


BillKSmith
Veteran

Oct 11, 2012, 8:00 PM


Views: 5121
Re: [gerble1000] importing txt file to array problem

Your new code should 'work perfectly', but it has a large number of serious style issues. Read my first post.

In addition, the perl style for loop is both faster and easier to understand than your c-style alternative.

Read the reference in my second post. Ask if you do not understand!
Good Luck,
Bill


gerble1000
User

Oct 11, 2012, 11:54 PM


Views: 5118
Re: [BillKSmith] importing txt file to array problem

i am unsure what you meant by "lvalue"


Laurent_R
Veteran / Moderator

Oct 12, 2012, 12:01 AM


Views: 5117
Re: [gerble1000] importing txt file to array problem

An lvalue is something (usually a variable) that can be put on the lest side of an assignment. In other words, something to which you can assign a value.