A simple question using SPLIT

Discussion in 'Perl Misc' started by Aman, Aug 20, 2005.

  1. Aman

    Aman Guest

    Hello everyone,

    I am new to perl and I am trying to split a line but can't seem to get
    it to work. Basically, the line is made up of multiple whitespaces.
    An example of a line is as follows and I want to split it into
    variables.

    1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1

    I tried using the following

    ($r,$b,$avm)=split(/\s/,$line);

    to split the first 3 rows but it does not work. Any ideas on how to
    accomplish this ? Ideally I would like to split each col. into a
    variable. Any help will be appreciated. Thank

    Aman
     
    Aman, Aug 20, 2005
    #1
    1. Advertising

  2. Aman

    Toni Erdmann Guest

    Aman wrote:

    > Hello everyone,
    >
    > I am new to perl and I am trying to split a line but can't seem to get
    > it to work. Basically, the line is made up of multiple whitespaces.
    > An example of a line is as follows and I want to split it into
    > variables.
    >
    > 1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1
    >
    > I tried using the following
    >
    > ($r,$b,$avm)=split(/\s/,$line);
    >
    > to split the first 3 rows but it does not work. Any ideas on how to
    > accomplish this ? Ideally I would like to split each col. into a
    > variable. Any help will be appreciated. Thanks
    >
    > Aman
    >


    ($r,$b,$avm) = split( /\s+/, $line );
    ^

    Toni
     
    Toni Erdmann, Aug 24, 2005
    #2
    1. Advertising

  3. Aman

    Joe Smith Guest

    Aman wrote:
    > Hello everyone,
    >
    > I am new to perl and I am trying to split a line but can't seem to get
    > it to work. Basically, the line is made up of multiple whitespaces.
    > An example of a line is as follows and I want to split it into
    > variables.
    >
    > 1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1
    >
    > I tried using the following
    >
    > ($r,$b,$avm)=split(/\s/,$line);
    >
    > to split the first 3 rows but it does not work.


    You need to re-read the docs on split().

    Take note of the section in `perldoc -f split` where it says:
    As a special case, specifying a PATTERN of space (' ') will...

    -Joe
     
    Joe Smith, Aug 24, 2005
    #3
  4. Aman

    Damian James Guest

    On Sat, 20 Aug 2005 16:07:57 GMT, Aman said:
    > ...
    > 1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1
    >
    > I tried using the following
    >
    > ($r,$b,$avm)=split(/\s/,$line);
    >
    > to split the first 3 rows but it does not work. Any ideas on how to
    > accomplish this ? Ideally I would like to split each col. into a
    > variable. Any help will be appreciated. Thanks


    That's because you're matching a single whitespace character. As others,
    have pointed out,

    ($r,$b,$avm)=split(/\s+/,$line);

    is probably what you meant. Note, however, that to match only the first 3 items,
    you'd probably want:

    ($r,$b,$avm,$rest)=split(/\s+/,$line, 4);

    I believe the confusion here is because this is the (often unspoken) default
    for split, so if you had done:

    $_ = '1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1';
    ($r,$b,$avm)=split;

    That would give the same result, but without the limit as above.

    --Damian.
     
    Damian James, Aug 25, 2005
    #4
  5. Aman

    Aman Guest

    Toni Erdmann <> wrote:

    > Aman wrote:
    >
    >> Hello everyone,
    >>
    >> I am new to perl and I am trying to split a line but can't seem to get
    >> it to work. Basically, the line is made up of multiple whitespaces.
    >> An example of a line is as follows and I want to split it into
    >> variables.
    >>
    >> 1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1
    >>
    >> I tried using the following
    >>
    >> ($r,$b,$avm)=split(/\s/,$line);
    >>
    >> to split the first 3 rows but it does not work. Any ideas on how to
    >> accomplish this ? Ideally I would like to split each col. into a
    >> variable. Any help will be appreciated. Thanks
    >>
    >> Aman
    >>

    >
    > ($r,$b,$avm) = split( /\s+/, $line );
    > ^
    >
    > Toni
    >
    >


    Thanks Toni, really appreciated. I will give this a shot.
     
    Aman, Aug 25, 2005
    #5
    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. Replies:
    2
    Views:
    470
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    702
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    218
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    246
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    620
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page