Re: want to assign default value to variable I assign from split but want to know optimal way to get this done: please help!

[email protected] (Rob Dixon)
Newsgroups perl.beginners
Message-ID <[email protected]>
Richard Lee wrote:
>
> Rob Dixon wrote:
>>     
>> while (<FILE>) {
>>   my ($file1,$file2,$file3,$file4,$file5,$file6,$file10,$f`ile25,$file27) =
>>       map $_||'default', (split /\|/)[3,4,6,7,12,40,41,42,43,46,56,64];
>> }
> 
> Thank you everyone.
> 
> I like this solution the best and thanks for pointing out that I was 
> misusing |= vs ||=

I'm not entirely happy with it as it stands. The numbered $file99 variables are
ugly and are screaming for an array, but without knowing more about what you're
doing I can't improve on what you originally wrote.

But I would prefer to see list of indices declared as a constant and the data
values defaulted separately, like this:

  use constant INDICES => (3,4,6,7,12,40,41,42,43,46,56,64);

  while (<FILE>) {
    my @data = map $_ || 'default', split /\|/;
    my ($file1,$file2,$file3,$file4,$file5,$file6,$file10,$file25,$file27) =
@data[(INDICES)];
  }

HTH,

Rob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.