in reply to Re: Comma separated list into a hash
in thread Comma separated list into a hash

Thanks to you and everyone else who didn't post full solutions as I sometimes get when I post here, better learning experience when you do most of it yourself.

I looked at split originall and that's where I got confused. You need to set up variables prior to splitting, it looks like you need to know how many splits there are so you can setup the variables.

How could I use a foreach and split a $string? This concept is way above me or is so trivial I'm thinking to hard. lol

Thanks everyone!

  • Comment on Re: Re: Comma separated list into a hash

Replies are listed 'Best First'.
Re: Re: Re: Comma separated list into a hash
by davido (Cardinal) on Apr 26, 2004 at 03:56 UTC
    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

Re: Re: Re: Comma separated list into a hash
by gsiems (Deacon) on Apr 26, 2004 at 03:53 UTC
    Note that split returns an array so you don't need to know "how many splits" are returned in order to use it.
      I don't understand. my ($this, $that) = split(/,/, $string); how is this an array? I had to create my variables before my split.

      Thanks.

        Split returns a list. What you do with that list is up to you. Some of the options are, you can assign that list to an array, or you can assign it into a list of scalar variables. Your example does the latter. You're assigning the output of split into two scalars.

        I wouldn't do it that way. The problem is that you can't be sure that there will only be two fields. You're better off assigning into an array, since arrays can hold zero to darn-near-infinite number of items.


        Dave

        I don't understand. my ($this, $that) = split(/,/, $string); how is this an array? I had to create my variables before my split.

        Ah, but in perl, arrays are a single variable that can hold a dynamic number of values! So you don't need to know beforehand how many values will result from the split; you can just do:

        my @words = split /,/, $string;

        Then, to figure out how many words resulted, you can just use @words in a scalar context:

        # find out information about the entire array print "found " . scalar( @words ) . " words\n"; # do stuff with each member of the array foreach my $word ( @words ) { print " word: $word\n"; }