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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
why cant i do this??



Jun 23, 2001, 6:56 PM

Post #1 of 4 (1414 views)
why cant i do this?? Can't Post

alright, heres what i want to do.
i want to open a file that is made like this:

then i want to split it
then i want to put all these values in an array, so the array would be something like this
@array = "modname1" "modman2" "modman" "whatever");

how the hell do i do this?
thanks for the help!

visit my web site at <a href=>here</a>

User / Moderator

Jun 23, 2001, 7:03 PM

Post #2 of 4 (1412 views)
Re: why cant i do this?? [In reply to] Can't Post

You'll want to use split. Something like ...

my @array; 
open FILE, "file.txt" or die "file.txt: $!";
while (<FILE>) {
push @array, split /;/


Jun 23, 2001, 8:57 PM

Post #3 of 4 (1408 views)
Re: why cant i do this?? [In reply to] Can't Post

hmmm... it doesnt seem to work, when i try to print our my array, i get nothing

visit my web site at <a href=>here</a>

Enthusiast / Moderator

Jun 24, 2001, 12:59 AM

Post #4 of 4 (1402 views)
Re: why cant i do this?? [In reply to] Can't Post


I really can't imagine what went wrong. Kanji's script snippet should work absolutely fine. I tried it, but I was even sure before trying. How did you try to print out the array?
Anyway, there hast to be a reason why it didn't work for you. Please try the following script and don't change anything except for the name of the file. It should print out a list of all elements separated by ; in the file. At least, it should print out anything if the file isn't empty.

#!/bin/perl -w 
use strict;

# put your filename here
my $file = 'myfile.txt';

# open the file
open FILE, $file or die "cannot open file '$file': $!\n";

# read each line, put elements separated by ';' into @array
my @array = map { chomp; (split ';') } <FILE>;

# close the file
close FILE;

# print all element from @array, each in a new line
print "$_\n" foreach @array;

If this doesn't print anything, I guess you have a problem that has nothing to do with the perl source.
Hope this helps.

-- Marcus


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

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