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:
why cant i do this??

 



ultimategamer2k
User

Jun 23, 2001, 6:56 PM

Post #1 of 4 (1154 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:
modname1;modman2;modman4;whatever

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=http://ultimategamer.cjb.net>here</a>


Kanji
User / Moderator

Jun 23, 2001, 7:03 PM

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

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

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



ultimategamer2k
User

Jun 23, 2001, 8:57 PM

Post #3 of 4 (1148 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=http://ultimategamer.cjb.net>here</a>


mhx
Enthusiast / Moderator

Jun 24, 2001, 12:59 AM

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

Hi,

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.

Code
#!/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