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



Rob Dixon wrote:
Richard Lee wrote:
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]
}

while doing above, what is the easiest way to make sure all the variable that's being given a value is true and if not
assign something default value (such as 'default' or 'X') ?

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?

while (<FILE>) {
my ($file1,$file2,$file3,$file4,$file5,$file6,$file10,$file25,$file27) =
map $_||'default', (split /\|/)[3,4,6,7,12,40,41,42,43,46,56,64];
}

Rob

Thank you everyone.

I like this solution the best and thanks for pointing out that I was misusing |= vs ||=

thank you all!!
.



Relevant Pages

  • Re: Parentheses
    ... Rob Dixon wrote: ... > with the array elements. ... of assigning the value of the it variable to a named scalar ... first thing inside the loop. ...
    (perl.beginners)
  • Re: Removing blank lines
    ... Have the data in an array and do ... On 3/12/07, Rob Dixon wrote: ... >>> I'm having a lot of trouble figuring out how to remove blank lines ... It could be spaces, tabs, or some other ...
    (perl.beginners)
  • Re: Temporary variables with map { }
    ... Rob Dixon wrote: ... contents of the @d array which is presumably required intact. ... Perl isn't a toolbox, but a small machine shop where you ...
    (perl.beginners)
  • Re: looping through a file
    ... Rob Dixon wrote: ... array @id_hits populated and I can search the log for the line and print it. ... The problem is it only finds the first match and that is it. ... I was thinking about a LABEL as I just read about that the other day. ...
    (perl.beginners)
  • Re: shorten code
    ... > Rob Dixon wrote: ... > Why do you have the second split operator ... The brackets create an array reference and fill it with the contents of ... One array ref per line of the original split. ...
    (perl.beginners)