in reply to Re^2: input tab delimited file
in thread input tab delimited file
I don't know if you will need to trim trailing spaces or not. But you should consider the following code...
I don't know what $line =~ s/&+/&/; equates to but I think this should be: chomp($line);. I hope that you've come to see the power of multiple variables to the left of the equals sign!! In many languages you have to write a bunch of stuff that essentially means something like thing 3 in the array is a "postal code". In Perl, we can just assign these variables names straight from the "get-go".#!/usr/bin/perl -w use strict; my $line = "tok1 \t \t\t tok4\n"; chomp ($line); #try running without this! my @x = my ($tok1, $tok2, $tok3, $tok4) = split(/\t/,$line); foreach my $token (@x) { print "token = $token..\n"; #.. is there to show blanks } __END__ prints: token = tok1 .. token = .. token = .. token = tok4..
Now we come the question about "undef" vars resulting from split. You have a lengthy section like $isbn='' unless defined($isbn);.
Run the above code with this line, adding $tok5:
You will see that you get a runtime warning about an undefined var. "Use of uninitialized value $token in concatenation (.)". This happens in the print and Perl keeps going and this is normally what you would want. You get some info that your database is corrupted and Perl does the best that it can.my @x = my ($tok1, $tok2, $tok3, $tok4, $tok5) = split(/\t/,$line);
The split() will not generate intermediate undef's, if that happens, the
undef will be at the end (ie not a position 3 or whatever). In the above
$tok5 is "undef" because we have exceeded the number of things returned by
the split().Let's say
that you want to detect "undef's" in the split and do something on your own.
Here is one way:
We see how many things that split() comes up with and assign that to @x. There won't be any "undef" values there. Then we see if we have enough defined values to satisfy the $var assignments (scalar value of the @x variable), if not then do what you want. This is just an example.my @x = split(/\t/,$line); die "I don't have enough stuff..need 5 tokens\n" if @x <5; my ($tok1, $tok2, $tok3, $tok4, $tok5) = @x;
In general if some field is completely "MIA" in the DB, it is field 3? 2? I mean if we are expecting to get 5 things and only get 4, then who knows what is missing and dealing with that can be very problematic! but the split/\t/ will generate a "", null for a "blank field", not an undef value.
Good luck and happy Perling! A fantastic language.
Update:Perl has an operator that I've never seen in another other language, |=, $varA |= "some text"; This statement means if $varA evaluates as *logical* false, then "some text" is assigned to it. In Perl, numeric 0, undef, "" all mean logical false. In some situations this "logical true OR" gizmo is a very nice thing, mainly dealing with undef or Null text strings.
|
|---|