Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Comma Delimitted Fields..

by thatguy (Parson)
on Oct 18, 2001 at 23:53 UTC ( #119780=note: print w/ replies, xml ) Need Help??


in reply to Comma Delimitted Fields..

split the color field ($data[1]) into an array and the print the colors like so:

#!/usr/bin/perl-Tw use strict; use Text::ParseWords; open(DATA,"basicdez.dat") || die "Cannot open datfile: $!\n"; my @data; my $i = 0; while (<DATA>) { chomp; last if /^"EOS"$/; { @data = &quotewords('\s+', 0, $_); } # if all fields exist (gets rid of PER issue) if (($data[0]) && ($data[1]) && ($data[2])) { my @colors=split(/,/, $data[1]); print "Name = $data[0]\n"; for (@colors){ # print each color print "Color = $_\n"; } print "Date = $data[2]\n"; } }

-phill


Comment on Re: Comma Delimitted Fields..
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Comma Delimitted Fields..
by Hofmator (Curate) on Oct 19, 2001 at 18:06 UTC

    Two small remarks:

    • for (@colors){ print "Color = $_\n"; }
      can lead to nasty surprises as you are overwriting $_ of the while loop. So either add local $_; before the loop or use a loop variable
      for my $color (@colors){ print "Color = $color\n"; }
    • if (($data[0]) && ($data[1]) && ($data[2])) {
      is quite restrictive. This doesn't allow for empty fields or lines without date. Alternatives would be if (@data == 3) or the next if suggestion by dragonchild. Of course, basicdez has to specify himself which of the alternatives fits his requirements.

    -- Hofmator

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://119780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2015-07-31 00:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls