Split problem.

Discussion in 'Perl Misc' started by Richard S Beckett, Nov 5, 2003.

  1. Guys,

    I have a line of text like this " 5 6 0 7 176
    0". i.e. 9 spaces before the 5.

    When I try to place the numbers into an array with split (my @temp = split
    (/\s*/, $input);), I get a blank element at the start. What am I doing
    wrong?

    Thanks.
    =-=-=-=-=-=-=-=-=-=-
    use strict;
    use warnings;
    my $line = " 5 6 0 7 176 0";
    my @array = split (/\s*/, $line);
    print "Starting...\n";
    foreach (0..$#array) {
    print "element $_ = $array[$_]\n";
    }
    print "Finished.\n";
    =-=-=-=-=-=-=-=-=-=-
    OUTPUT:
    Starting...
    element 0 =
    element 1 = 5
    element 2 = 6
    element 3 = 0
    element 4 = 7
    element 5 = 1
    element 6 = 7
    element 7 = 6
    element 8 = 0
    Finished.
    --
    R.
    GPLRank +79.699
     
    Richard S Beckett, Nov 5, 2003
    #1
    1. Advertising

  2. In article <bob4es$t49$>, Richard S Beckett wrote:
    > Guys,
    >
    > I have a line of text like this " 5 6 0 7 176
    > 0". i.e. 9 spaces before the 5.
    >
    > When I try to place the numbers into an array with split (my @temp = split
    > (/\s*/, $input);), I get a blank element at the start. What am I doing
    > wrong?


    If a list is separated by colons like this:

    1:2:3:4:5

    then we can agree on that there are five items in the list.
    What about this list then:

    :1:2:3:4:5

    Perl believes that the first element is an empty element since
    we've separatedt it from the second element, which is '1'.

    The "solution" would be to remove all flanking separators:

    $list =~ s/^:*//;
    $list =~ s/:*$//;

    and then split it

    @arr = split /:/, $list;


    This applies to your choise of seperator as well.


    --
    Andreas Kähäri
     
    Andreas Kahari, Nov 5, 2003
    #2
    1. Advertising

  3. On Wed, 5 Nov 2003 15:18:27 -0000, Richard S Beckett wrote:
    > Guys,
    >
    > I have a line of text like this " 5 6 0 7 176
    > 0". i.e. 9 spaces before the 5.
    >
    > When I try to place the numbers into an array with split (my @temp = split
    > (/\s*/, $input);), I get a blank element at the start. What am I doing
    > wrong?


    If you "perldoc -f split", you'll see:

    As a special case, specifying a PATTERN of space (' ') will split on
    white space just as "split" with no arguments does.

    So try:

    my @tmp = split(" ",$input);
     
    John J. Trammell, Nov 5, 2003
    #3
  4. > > Guys,

    > > I have a line of text like this " 5 6 0 7 176


    > > 0". i.e. 9 spaces before the 5.


    > > When I try to place the numbers into an array with split (my @temp =

    split

    > > (/\s*/, $input);), I get a blank element at the start. What am I doing


    > > wrong?




    > If a list is separated by colons like this:


    > 1:2:3:4:5


    > then we can agree on that there are five items in the list.


    > What about this list then:


    > :1:2:3:4:5


    > Perl believes that the first element is an empty element since


    > we've separatedt it from the second element, which is '1'.


    > The "solution" would be to remove all flanking separators:


    > $list =~ s/^:*//;


    > $list =~ s/:*$//;


    > and then split it


    > @arr = split /:/, $list;


    > This applies to your choise of seperator as well.


    Aaaaaaah!

    Thanks Andreas.

    R.
     
    Richard S Beckett, Nov 5, 2003
    #4
  5. > If you "perldoc -f split", you'll see:
    > As a special case, specifying a PATTERN of space (' ') will split on
    > white space just as "split" with no arguments does.
    > So try:
    > my @tmp = split(" ",$input);


    I did read it, but didn't really understand it. Sorry.

    So you're saying that:
    my @tmp = split(" ",$input);

    is the equivalent of:
    $input =~ s/^\s*//;
    my @tmp = split (/\s+/, $input);

    ?

    The thing that threw me was that there are different numbers of spaces
    between the numbers in $input, so I didn't think that would work.

    Thanks.

    R.
     
    Richard S Beckett, Nov 5, 2003
    #5
  6. [posted & mailed]

    On Wed, 5 Nov 2003, Richard S Beckett wrote:

    >I have a line of text like this " 5 6 0 7 176
    >0". i.e. 9 spaces before the 5.
    >
    >When I try to place the numbers into an array with split (my @temp = split
    >(/\s*/, $input);), I get a blank element at the start. What am I doing
    >wrong?


    The thing you're doing wrong doesn't have to do with the leading empty
    field. You should be using split(/\s+/, $input), because \s* will match
    ZERO spaces (which is why '176' is split into 1, 7, and 6).

    The split function WILL return empty leading fields if they exist, so
    using split(/\s+/, $input) will still return an empty leading field.

    However, there's a special case: split(' ', $input). This will split on
    multiple spaces, AND ignore a leading empty field.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Nov 5, 2003
    #6
  7. [attributions fixed]
    On Wed, 5 Nov 2003 16:48:12 -0000, Richard S Beckett wrote:
    > [Trammell wrote:]
    >> If you "perldoc -f split", you'll see:
    >> As a special case, specifying a PATTERN of space (' ') will split on
    >> white space just as "split" with no arguments does.
    >> So try:
    >> my @tmp = split(" ",$input);

    >
    > I did read it, but didn't really understand it. Sorry.
    >
    > So you're saying that:
    > my @tmp = split(" ",$input);
    >
    > is the equivalent of:
    > $input =~ s/^\s*//;
    > my @tmp = split (/\s+/, $input);
    >
    > ?


    Again from perldoc -f split:

    A "split" on "/\s+/" is like a "split(' ')" except that any leading
    whitespace produces a null first field. A "split" with no arguments
    really does a "split(' ', $_)" internally.

    ph33r my 1337 perldoc sk33lz!
     
    John J. Trammell, Nov 5, 2003
    #7
  8. Richard S Beckett wrote:
    >
    > I have a line of text like this " 5 6 0 7 176
    > 0". i.e. 9 spaces before the 5.
    >
    > When I try to place the numbers into an array with split (my @temp = split
    > (/\s*/, $input);), I get a blank element at the start. What am I doing
    > wrong?


    Use the match operator to get the data you want instead of using split
    to remove the data you don't want. :)

    my @array = $line =~ /\d/g;

    Or if you really want ( 176 ) instead of ( 1, 7, 6 ) then:

    my @array = $line =~ /\d+/g;



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 9, 2003
    #8
    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:
    618
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page