Help with Perl newbie..the $/ separator

Discussion in 'Perl' started by Javier, Jan 15, 2004.

  1. Javier

    Javier Guest

    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.
     
    Javier, Jan 15, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Jan 15, 2004
    #2
    1. Advertising

  3. Javier

    Stoco Guest

    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.
     
    Stoco, Jan 15, 2004
    #3
  4. Javier

    Javier Guest

    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
     
    Javier, Jan 16, 2004
    #4
  5. Javier

    Javier Guest

    (Stoco) wrote in message news:<>...
    > 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.
     
    Javier, Jan 16, 2004
    #5
  6. 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
     
    Gunnar Hjalmarsson, Jan 16, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,839
  2. Kevin Spencer

    Re: Separator in panel

    Kevin Spencer, Aug 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    430
    Kevin Spencer
    Aug 20, 2003
  3. DC Gringo
    Replies:
    3
    Views:
    6,196
    mikeb
    Aug 19, 2004
  4. Kaja
    Replies:
    0
    Views:
    619
  5. Sak ..
    Replies:
    5
    Views:
    298
    Sak ..
    Feb 10, 2010
Loading...

Share This Page