Jose Malacara said:
> 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.

Without having tested, I would suggest:

> #!/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);

You should uncomment this line.

> foreach $line (@datafile) {
>         if ($line =~ /$person/) {
>                 @words = split(",", $line);
>                 $words[2] = $city;
>                 $" = ",";

$" is a global variable.  If you want to use it in this way, you should
probably localise it.  This is why you are getting those commas at the
start of the lines in the final print.

    local $" = ",";

>                 $line = "@words";

and you'll need to add your newline here.

>         } # end if
>         push(@newdata, $line);
> } # end foreach
> print "@newdata";

It might be easier to do the transformations and prints while looping
through the file, rather than using @datafile and @newdata.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net




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

Reply via email to