CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



gerble1000
User

Oct 11, 2012, 2:20 AM

Post #1 of 15 (1697 views)
importing txt file to array problem Can't Post

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

Post #2 of 15 (1685 views)
Re: [gerble1000] importing txt file to array problem [In reply to] Can't Post

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

Post #3 of 15 (1682 views)
Re: [BillKSmith] importing txt file to array problem [In reply to] Can't Post

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

Post #4 of 15 (1679 views)
Re: [gerble1000] importing txt file to array problem [In reply to] Can't Post


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

Post #5 of 15 (1675 views)
Re: [FishMonger] importing txt file to array problem [In reply to] Can't Post

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

Post #6 of 15 (1671 views)
Re: [gerble1000] importing txt file to array problem [In reply to] Can't Post

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

Post #7 of 15 (1668 views)
Re: [FishMonger] importing txt file to array problem [In reply to] Can't Post

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

Post #8 of 15 (1665 views)
Re: [gerble1000] importing txt file to array problem [In reply to] Can't Post

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

Post #9 of 15 (1664 views)
Re: [FishMonger] importing txt file to array problem [In reply to] Can't Post

it was as if the program was in an infinate loop


BillKSmith
Veteran

Oct 11, 2012, 9:40 AM

Post #10 of 15 (1657 views)
Re: [gerble1000] importing txt file to array problem [In reply to] Can't Post

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

Post #11 of 15 (1656 views)
Re: [FishMonger] importing txt file to array problem [In reply to] Can't Post

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

Post #12 of 15 (1653 views)
Re: [BillKSmith] importing txt file to array problem [In reply to] Can't Post

i was close then :)


BillKSmith
Veteran

Oct 11, 2012, 8:00 PM

Post #13 of 15 (1643 views)
Re: [gerble1000] importing txt file to array problem [In reply to] Can't Post

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

Post #14 of 15 (1640 views)
Re: [BillKSmith] importing txt file to array problem [In reply to] Can't Post

i am unsure what you meant by "lvalue"


Laurent_R
Veteran / Moderator

Oct 12, 2012, 12:01 AM

Post #15 of 15 (1639 views)
Re: [gerble1000] importing txt file to array problem [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives