Re: [MacPerl-WebCGI] well split!!!
[email protected] Thu, 8 Mar 2001 12:41:21 +0900
| Newsgroups | perl.macperl.webcgi |
|---|---|
| Message-ID | <v04011700b6cc9e37ea80@[211.19.94.253]> |
hi Ted
>........ (...)=split(...) that I never saw
>before. Just look up the split() function and see how it's defined.
>In no place I've found does it say that it can be used in such a
>fashion, but that looks like what perl is all about. It's a different
>way to view things than I am used to, but I'm getting the idea.
It's the same principal as
>>@array= ("one","two","three");
you have a stream of values moving from right to left
$string="one=two=three";
@array=split('=',$string);
the only difference is that the stream is coming from a function
>>Why bother doing it like this?
>>@data {@data} =localtime(time);
>>you don't have to remember the position of the data in the array, you can
>>just call it by name.
>
>Thanks, the first two makes sense, but that last one I'll have to
>give some serious thought.
try thinking:
@data_names
=("secs","mins","hour","month_day","month","year","wday","yday","isdst");
@data{@data_names} =localtime(time);
or
@data_names=("database_id","category","price","description","item_number")
@data_base{@data_names}=split(@data);
You could write this as:
$data_record="number 1,computers,2.00,kind of big,item134";
@data=split(',',$data_record);
@data_names=("database_id","category","price","description","item_number");
for $item (@data_names){
$data_base{$item}=shift(@data);
}
which is what's (sort of) happening above but in batch mode. The advantage
of doing this is of course you can get at the information like this:
for $item (keys(%data_base)){
print "$item: $data_base{$item}\n";
}
or
print "database_id: $data_base {database_id}\t item_number: $data_base {
item_number}\n";
rather than try to remember the array number :
print "database_id: $data_record [0]\t item_number: $data_record[4]\n";
The choice is up to you and the needs of the situation.
Robin