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:
print $data[1][1];

 



Pro_4
User

Jun 30, 2001, 5:45 PM

Post #1 of 5 (831 views)
print $data[1][1]; Can't Post

I had asked a long time ago how to read information out of a database and print it up. Well (i believe freddo) gave me the reply if i want to print only one part of it i should do this:

open (DB,"db.txt") or die "Cannot open db.txt: $!\n\n";
my $i = 0;
my @data;
while (<DB>) {
@{ $data[$i] } = split(/\|/);
}
close(DB);

So when i typed print $data[1][1]; it would print the whatever was the second thing in the second line. But for some reason this doesnt seem to be working...


Yes i am a palm tree. What are you plantist or something!?


mhx
Enthusiast / Moderator

Jul 1, 2001, 12:56 AM

Post #2 of 5 (826 views)
Re: print $data[1][1]; [In reply to] Can't Post

Hi Pro_4,

the code doesn't work because the line number $i is not incremented. @data will always contain only the last line. So the code would work if you added this:

Code
open (DB,"db.txt") or die "Cannot open db.txt: $!\n\n"; 
my $i = 0;
my @data;
while (<DB>) {
@{ $data[$i++] } = split(/\|/);
}
close(DB);

Nevertheless, you can write this without the use of an extra line counter variable:

Code
my $db = 'db.txt'; 
open DB, $db or die "can't open '$db' for reading: $!\n";
my @data = map [split /\|/], <DB>;
close DB;

-- Marcus



Pro_4
User

Jul 1, 2001, 6:56 AM

Post #3 of 5 (824 views)
Re: print $data[1][1]; [In reply to] Can't Post

Thanks alot mhx. That worked really well.

I noticed two things that made me start to wonder:

1. close DB;

I always thought that it had to be close(DB);

2. while (<DB>) { @{ $data[$i++] } = split(/\|/); }

If you can do the i++ in while statements and it will properly increment it, will it work the same way in for's or foreach statements.

Thanks again :)

Yes i am a palm tree. What are you plantist or something!?


japhy
Enthusiast

Jul 1, 2001, 7:47 AM

Post #4 of 5 (820 views)
Re: print $data[1][1]; [In reply to] Can't Post

Perl's functions are not very strict. You don't need to parenthesize, if it's not ambiguous what you're doing.



Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


mhx
Enthusiast / Moderator

Jul 1, 2001, 8:02 AM

Post #5 of 5 (820 views)
Re: print $data[1][1]; [In reply to] Can't Post


In Reply To
I always thought that it had to be close(DB);

No, actually, the brackets are optional. (as japhy mentioned yet)

In Reply To
If you can do the i++ in while statements and it will properly increment it, will it work the same way in for's or foreach statements.

The suffixed double plus operator (post-increment operator) will always increment the variable. So $i++ will always increment $i, no matter if it's inside any loop or not. There's also a pre-increment operator, ++$i, which will increment $i, too. The difference between these two is just that when the variable to be incremented is used in the expression (e.g. as an index into an array), the postfixed ++ will increment after usage, while the prefixed ++ will increment before. An example should make this a bit clearer:

Code
my $i   = 0; 
my @ary = 'A'..'C';

print "$ary[++$i]\n"; # prints B
print "$i\n"; # prints 1
print "$ary[$i++]\n"; # prints B
print "$i\n"; # prints 2

-- Marcus



(This post was edited by mhx on Jul 1, 2001, 7:06 AM)

 
 


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

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