Re: [MacPerl-WebCGI] well split!!!
[email protected] (Bruce Van Allen) Wed, 7 Mar 2001 09:45:25 -0800
| Newsgroups | perl.macperl.webcgi |
|---|---|
| Message-ID | <p05010401b6cc1b9841c4@[205.179.215.44]> |
At 10:16 AM -0500 3/7/01, [email protected] wrote: >-Bruce: > >You said: > >> my ($database_id, $category, $variable_name, $price, >>$description, $item_number) = split(/\t/); > >Where do you people find statements like -- > > my ($database_id, $category, $variable_name, $price, $description, >$item_number) = split(/\t/); > >-- anyway? I have numerous books on Perl (including the "Perl Black >Book") and none of them come even close to describing anything like >that in providing a definition for split(). > >However, in a single book, namely "CGI How-To", the author uses the >following statement: > > >($database_id,$category,$variable_name,$price,$name,$description,$item_number)=split(/\|/,$_); > >But, please note the absence of "my". > >It's like I'm trying to learn how to program a box of alphabet >cereal. You people throw out stuff that's not documented in any of >the reference material I have. It's not a complaint, just an >observation. > Tedd -- On the question of statements like my ($database_id, $category, $variable_name, $price, $description, $item_number) = split(/\t/); this is a very common Perl idiom. It's based on the idea that split() returns a list. The other thing that's going on is that 'my' sets the scope of the variables to which it's assigning the values of the list from split(). Think of the scope as being where those variables hold particular meanings, which in this case is only within each cycle of the "while (<DATA>)" loop, because 'my' is freshly invoked each time around the loop. As for the other problem with your data, have you checked for the type of line endings, as someone else suggested? If the whole file is included in one chunk from <DATA>, then Perl is not finding the "line ending" it expects. This is either because the file uses different endings from what's native to the OS Perl is working in, or your script has changed the value of $/, Perl's internal input record separator, or "line ending". How about posting a few lines of your data? Also, just to be sure, in the examples I've given back to you, I didn't write out everything, so here goes: # Script includes everything to and including the __END__ line: # Set your #! line as you usually do #perl -w use strict; my $datafile = 'data.txt'; # use your filename open DATA, "$datafile" or &open_error($datafile); while (<DATA>) { next if /^$/; my ($database_id, $category, $variable_name, $price, $description, $item_number) = split(/\t/); print <<ROW; <TR> <TD>$database_id</TD> <TD>$category</TD> <TD>$variable_name</TD> <TD>\$$price</TD> <TD>$description</TD> <TD>$item_number</TD> </TR> ROW } close DATA; __END__ If you only get one line of output, then, indeed, see above re the proper "line endings". The whole file would be split on tab, but only the first six members of the resulting list are captured in the six variables above. -- - Bruce __bruce_van_allen__santa_cruz_ca__