Help with Perl newbie..the $/ separator

J

Javier

Greetings..
my file looks something like this..
item1 item2 6
itemA itemB 3
itmeX item1 1

I'm reading a list
MYFILE = "/myItems.txt";
$/="\n";
while(<MYFILE>){
push(@list,$_);
}

while this gives me each line ..
@list[0] item1 item2 6
@list[1] itemA itemB 3

what I need is the list to be like this
@list[0] item1
@list[1] item2
@list[3] 6
@list[4] itemA
@list[5] itemB

my problem is that I can only define one separator at the time.
How can I tell the parser to use "\n" and " " as separators?
Thanks for your help.

Javier.
 
G

Gunnar Hjalmarsson

Javier said:
my file looks something like this..
item1 item2 6
itemA itemB 3
itmeX item1 1

I'm reading a list
MYFILE = "/myItems.txt";
$/="\n";
while(<MYFILE>){
push(@list,$_);
}

while this gives me each line ..
@list[0] item1 item2 6
@list[1] itemA itemB 3

what I need is the list to be like this
@list[0] item1
@list[1] item2
@list[3] 6
@list[4] itemA
@list[5] itemB

my problem is that I can only define one separator at the time.
How can I tell the parser to use "\n" and " " as separators?

What happened when you tried $/=" "; ?
 
S

Stoco

I think this may work... At bit long, but it does the trick.

open (MYFILE, "<Text-1.txt");
my @contents = <MYFILE>;
close (MYFILE);

my @out;
foreach my $line (@contents){
chomp($line);
my @row = split (/ /, $line);
@out = (@out, @row);
}
1;

John
 
J

Javier

Gunnar Hjalmarsson said:
Javier said:
my file looks something like this..
item1 item2 6
itemA itemB 3
itmeX item1 1

I'm reading a list
MYFILE = "/myItems.txt";
$/="\n";
while(<MYFILE>){
push(@list,$_);
}

while this gives me each line ..
@list[0] item1 item2 6
@list[1] itemA itemB 3

what I need is the list to be like this
@list[0] item1
@list[1] item2
@list[3] 6
@list[4] itemA
@list[5] itemB

my problem is that I can only define one separator at the time.
How can I tell the parser to use "\n" and " " as separators?

What happened when you tried $/=" "; ?

When I use a space as separtor,
@list[3] turns out to be 6\nitemA
because a eoln(\n) is not considered a separator
 
J

Javier

I think this may work... At bit long, but it does the trick.

open (MYFILE, "<Text-1.txt");
my @contents = <MYFILE>;
close (MYFILE);

my @out;
foreach my $line (@contents){
chomp($line);
my @row = split (/ /, $line);
@out = (@out, @row);
}
1;

John

Thanks a bunch..worked on first try.

Javier.
 
G

Gunnar Hjalmarsson

Javier said:
Gunnar said:
What happened when you tried $/=" "; ?

When I use a space as separtor,
@list[3] turns out to be 6\nitemA
because a eoln(\n) is not considered a separator

Suppose you mean $list[2] ... but yes, you are right. I thought it
'worked' for me, but I just fooled myself. :(

Using split() works of course, as you know by now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top