Perl array/loop question!

Hi,

There is probably an easy solution to this but I can't figure it out. I am looking to:

- take a CSV file into an array and...
- loop through the array and split fields into variables
[b]- if the array field is empty then set the variable to "N/A"[/b]

[italic][b]Note: it is only the setting the $variable to "N/A" that I can not get[/b][/italic]

ie:

foreach $var (@list) {

($name,$date,$size, etc...)=split(/,"/,$var);

}

How would I set $date to "N/A" if the field in the array is empty??

so to produce:

$name = Jim
$date = N/A
$size = small

I Hope this makes sense to someone and I hope there is an easy fix!

THANKS!!!

Comments

  • Maybe u r looking for someithing like that:

    A quick way to set a variable to a default value if it evaluates to false is to use the ||= operator.

    $color ||= 'black';

    Will set $color to black if it is empty, undefined or contains zero. Otherwise it will retain its original value.

  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion