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

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

Thanks.

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

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

Re^5: Comma separated list into a hash
by tkil (Monk) on Apr 26, 2004 at 05:59 UTC
    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"; }