Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Another question about the split function

by RaduH (Scribe)
on Dec 11, 2007 at 22:38 UTC ( [id://656504]=perlquestion: print w/replies, xml ) Need Help??

RaduH has asked for the wisdom of the Perl Monks concerning the following question:

I an using split to break strings like these:
Resource Group: gr_shared_fs rs_ipaddr (heartbeat::ocf:IPaddr): Started lapg2-a rs_raid1_shared_fs (heartbeat::ocf:Raid1): Started lapg2- +a rs_fs_shared (heartbeat::ocf:Filesystem): Started lapg2- +a
into blocks separated by space. Please note the white space in front of the rs_ starting strings (below "Reso" from the first line). That white space may or may not be there. I need to split the strings and I am using this:
my @statusLineFields = split(/\s+/, $statusLine);
but it seems if I do have that white space there the first element in @statusLineFields is whitespace. If the input lines do not have any leading space then the first element is some rs_ string. I really need to ignore that white space and have the first element be the rs_ string somehow.

I have the feeling this is a 1-line quick and easy answer but I've been running circles around my tail and couldn't figure it. In addition to this, once I split the line, the elements of that array (with or without the first one being whitespace) are surrounded by one blank space on each side. That "Started" is actually " Started ". Why is this and how do I get rid of it?

Thanks!

Replies are listed 'Best First'.
Re: Another question about the split function
by FunkyMonk (Chancellor) on Dec 11, 2007 at 23:10 UTC
    If you're having a problem with split, the official documentation is the first place to look. You can access it...
    • from the command line by typing perldoc -f split (*nix and Windows)
    • in HTML (ActivePerl on Windows) via start-->Programs-->ActivePerl-->Documentation
    • by typing doc://split into the PerlMonks search box
    • by Googling for perldoc split.

    Any of the links I've posted will also take you there.

    Anyway, split says (with my emphasis)...

    If EXPR is omitted, splits the $_ string. If PATTERN is also omitted, splits on whitespace (after skipping any leading whitespace)

    and ...

    As a special case, specifying a PATTERN of space (' ' ) will split on white space just as split with no arguments does
    So, all you need to do is
    my @statusLineFields = split(' ', $statusLine);

      If EXPR is omitted, splits the $_ string. If PATTERN is also omitted, splits on whitespace (after skipping any leading whitespace)
      As a special case, specifying a PATTERN of space (' ' ) will split on white space just as split with no arguments does

      Those little factoids escaped me. I suppose it's time to start reading 'Programming Perl' for the third (or is it fourth?) time. No matter how many times I read it I always pick up something new

      90% of every Perl application is already written.
      dragonchild
Re: Another question about the split function
by pfaut (Priest) on Dec 11, 2007 at 22:53 UTC

    Remove the leading spaces before doing the split.

    $statusLine =~ s/^\s+//;
    90% of every Perl application is already written.
    dragonchild
Re: Another question about the split function
by Fletch (Bishop) on Dec 11, 2007 at 22:54 UTC

    If leading whitespace really isn't significant then why not just remove it before further processing (e.g. $line =~ s/^\s+//;)?

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Another question about the split function
by almut (Canon) on Dec 11, 2007 at 22:57 UTC

    Try the magical ' '

    my @statusLineFields = split(' ', $statusLine);
Re: Another question about the split function
by downer (Monk) on Dec 11, 2007 at 23:15 UTC
    why not just try a  $_ =~ s/^\s//gm as a pre-process step?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://656504]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found