The input to split is (usually) a Regex, followed by a string to split. And the output (the return value) is a list of each individual item split out.

So the idea is that you will put your string to be 'parsed' into a scalar variable, say, for example, $string. You will then assign the output of split to a list.

If I were splitting a string containing numbers seperated by whitespace, I might do this:

my $string = "1 3 5 7 11 13"; my @numbers = split /\s+/, $string;

Now @numbers is an array holding six elements, each element containing one number.

You don't have to split into an array though. You can split such that the return value is used to populate 'foreach' with data to iterate over. My previous example might look like this:

my $string = "1 3 5 7 11 13"; foreach my $number ( split /\s+/, $string ) { # Do some stuff with $number. # Number will contain one number from the list each time # through the loop. }
Hope this helps...


Dave


In reply to Re: Re: Re: Comma separated list into a hash by davido
in thread Comma separated list into a hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.