On 21 Nov 2002, Jose Malacara wrote:

> Can someone please tell me what I'm doing wrong here?
> 
> I have a data file that looks like this:
> 
> jason,texas,austin
> tim,denver,colorado
> jose,oregon,portland
> 
> 
> And a script to update the last field and output the results with the
> new city:
> 
> 
> #!/usr/bin/perl -w
> 
> open(DATAFILE, "datafile") || die "Unable to open file!\n";
> @datafile = <DATAFILE>;
> close(DATAFILE);
> 
> print "Enter a name (jason, tim, jose): ";
> $person = <STDIN>;
> chomp ($person);
> 
> print "Enter their new city: ";
> $city = <STDIN>;
> #chomp ($city);
> 
> foreach $line (@datafile) {
>         if ($line =~ /$person/) {
>                 @words = split(",", $line);
>                 $words[2] = $city;
>                 $" = ",";

You have set $" = "," here, this is the reason you are getting an extra 
','. It is recommended to make changes on special vars like this
local $" = ",";
when this if finishes $" will be restored to it's original value

>                 $line = "@words";

Or better still use join
$line = join (',', @words);
perldoc -f join

>         } # end if
>         push(@newdata, $line);
> } # end foreach
> print "@newdata";
> 
> 
> I am expecting to see the output in the same original format, but I keep
> getting this instead:
>
> jason,texas,austin
> ,tim,denver,boulder
> ,jose,oregon,portland
> 
> I am just not sure why all lines after the first begin with a space
> (thus a comma)
> 
> Any help would be greatly appreciated!
> 
> Jose


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to