in reply to Comma separated list into a hash

please do NOT solve this entire thing for me...

Bravo!

There are several issues to consider.

First, is it possible that each of your substrings will contain commas themselves? From the looks of your list, the answer is no. Second, do you want punctuation (such as the trailing '?' question mark) to be included as part of that word?

The simplest solution will be to have a look at split, and use it with a comma followed by zero-or-more spaces as the delimiter.

Then, if you desire to strip away punctuation such as that trailing question mark, you can use a substitution operator (s///). See perlrequick andperlretut for details there. As a matter of fact, for simple character by character stripping of non-word characters, you can also use tr///. That's also covered in perlrequick if I'm not mistaken. Those are also covered in perlop and perlre, though those documents are more difficult reads.

But if you're dealing, instead, with a more complex dataset, where commas may be included within the text so long as the individual text element is itself quoted, then you had better look at Text::CSV where it's done right. That's an easy thing to goof up if you try to do it all with home-rolled regexps.

I hope this gets you going in the right direction without giving away the solution. The biggest step you can take in the right direction while learning Perl is learning to utilize the POD's (Plain Old Documentation that comes with Perl).

I'd like to see what you come up with, so if you get stuck, or once you complete it, post a followup to this thread so we can have a look at what you were able to put together. Then if you want, we can critique it for you to enhance your learning experience. :)


Dave

Replies are listed 'Best First'.
Re: Re: Comma separated list into a hash
by Anonymous Monk on Apr 26, 2004 at 03:45 UTC
    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!

      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

      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.