[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Problem - rewrite and simplify



#    Data:

#	apple, banana, cabbage, dillpickle, eggplant, fig, grape

#	Desired result:

#	Break the data into fields; fields are separated by ", "
#	Store each field separately

# Problem -
	# The original code will lose the last entry on the line iff the data
	# file ever contains more than one line.

# Original code:

	  #
	  # Initializations
	  #
	  $count = 0;
	  @ids = ();
	  #
	  # Read the Cloneids into an array called ids
	  #
	  while ($line = <CLONEIDSLIST>) {
	    chop ($line);
	    until (index($line, ', ',0)< 0) {
	         $ids[$count++] = substr($line,0,index($line,', ',0));
	         $line = substr($line, index($line, ', ',0) + 2);
	    }
	    $ids[$count] = substr($line,0);
	  }



# Replacement code :-)

	  #
	  # Read the Cloneids into an array called ids
	  #
	  while ($line = <CLONEIDSLIST>) {
	     chop($line);
	     push(@ids, split(/, /, $line));
	  }


# The replacement code never loses an entry.

# I didn't see the need to initialize the (guaranteed to start out
# empty)  @ids.

# Better ideas??
-- --
        |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
  ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
       |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
      '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.com

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org