Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Help with Perl newbie..the $/ separator

Reply
Thread Tools

Help with Perl newbie..the $/ separator

 
 
Javier
Guest
Posts: n/a
 
      01-15-2004
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.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-15-2004
Javier wrote:
> 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 $/=" "; ?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Stoco
Guest
Posts: n/a
 
      01-15-2004
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
-------------
There are 10 types of people in the world.
Those who understand binary, and those who don't.
 
Reply With Quote
 
Javier
Guest
Posts: n/a
 
      01-16-2004
Gunnar Hjalmarsson <> wrote in message news:<Y%yNb.78362$>...
> Javier wrote:
> > 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
 
Reply With Quote
 
Javier
Guest
Posts: n/a
 
      01-16-2004
(Stoco) wrote in message news:<. com>...
> 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
> -------------
> There are 10 types of people in the world.
> Those who understand binary, and those who don't.


Thanks a bunch..worked on first try.

Javier.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-16-2004
Javier wrote:
> Gunnar Hjalmarsson wrote:
>>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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change stdin separator (perl vs ruby) Sak .. Ruby 5 02-10-2010 04:19 PM
Compare validator does not allow thousand separator for double datatype. Kaja ASP .Net 0 09-09-2004 05:28 AM
need url parameter separator "&" not "&amp;" DC Gringo ASP .Net 3 08-19-2004 08:39 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
Re: Separator in panel Kevin Spencer ASP .Net 0 08-20-2003 03:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57