Perl

Moderators: Jonathan
Number of threads: 1257
Number of posts: 3636

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Perl array/loop question! Posted by robersha on 14 Jul 2009 at 2:41 PM
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
- if the array field is empty then set the variable to "N/A"

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

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!!!

Report
Re: Perl array/loop question! Posted by werrie on 17 Oct 2010 at 4:47 AM
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.

Report
This post has been deleted. Posted by werrie on 17 Oct 2010 at 4:48 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.