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] ("John W. Krahn")
Newsgroups perl.beginners
Message-ID <[email protected]>
Jenda Krynicky wrote:
> From: Richard Lee <[email protected]>
>>
>> I was doing (after the while loop)
>>
>> $file |= 'default'
>> $file2 |= 'default2'
>> $file3 |= 'default3'
>>
>> but I stopped and thought this cannot be so repetitious
>>
>> so I didn't want to but tried( I didn't want to put them in array since 
>> I need to use individual named variable later)
>>
>> while (<FILE>) {
>>        my @array = (split( /\|/, $_))[3,4,6,7,12,40,41,42,43,46,56,64]
>> }
>> for (@array) {
>>      $_  |= 'default';
>> }
>>
>> but is that the best way to do this?
> 
> So are the defaults the same or not?
> 
> If they are you can do something like
> 
>  while (<FILE>) {
>   
> my($file1,$file2,$file3,$file4,$file5,$file6,$file10,$file25,$file27)
>          = (split( /\|/, $_))[3,4,6,7,12,40,41,42,43,46,56,64];
>   
> for($file1,$file2,$file3,$file4,$file5,$file6,$file10,$file25,$file27)
>  {
>    $_  |= 'default';
>   }
>  }
> 
> Actually this seems to work as well:
> 
>  while (<FILE>) {
>   $_  |= 'default' for 
> (my($file1,$file2,$file3,$file4,$file5,$file6,$file10,$file25,$file27)
>          = (split( /\|/, $_))[3,4,6,7,12,40,41,42,43,46,56,64]);
>   # and some code using $file1, ...
>  }
> 
> If you want a different default for each field, you can't IMHO do 
> better than the original code.

Please note that |= does not do what you appear to think it does, you 
really want to use ||= instead.

$ perl -e'
use Data::Dumper;
$Data::Dumper::Useqq = 1;
$x = "one";
print Dumper $x;
$x |= "two";
print Dumper $x;
'
$VAR1 = "one";
$VAR1 = "\177\177o";



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall
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.