> [EMAIL PROTECTED] wrote:
> 
> > Hi All 
> >
> >Prob: I want to convert all the elememts of an array to ascalor variable tha
> t I can use latter on in my code 
> >e.g. 
> >
> >my @array=qw/balli johney bobby/;
> >now here i need 3 different variable with the above values i.e.
> >my $name1=balli ;
> >my $name2=johney ;
> >my $name3=bobby ;
> >
> >Soln so far: 
> >
> >my ($i) ;
> >for ($i=0; $i<@array;$i++)
> >{
> >$name=$array[$i] ; # lost here is to how to have different $name each time a
> lso how can make these 
> >
> my $i;
> for ($i=0; $i<@arrayl $i++) {
>     $name$i=$array[$i];
> }
> 
> That way you don't need to know in advance how many $name variables you
> need.
> 

NO, NO, NO, NO, NO!!!!!!!!!!!!!!!!!

First:  Your code doesn't even work.

Second:  It is not actually SOLVING anything.

variables of the form 
 $name0,  $name1, $name2

... do not provide **ANY** functional difference from 

   $array[0],  $array[1], and $array[2]

Write that on the blackboard fifty times.

That is to say: $array[0] is a first-class citizen, just as good as
$name0 -- iterating in the worst possible way[1] across an array to
create copies of the data provides NO additional functionality to the
code.

I suspect if the original poster could read and write English he
(she?) would be able to explain what was really needed.

There *are* times when exploding an array into a series of separately
named scalars *does* make sense.

Think how stupid the following is:

my @array;

($array[0], $array[1], $array[2], $array[3],
 $array[4], $array[5], $array[6], $array[7], $array[8] ) = localtime(time);

But, if we rewrite it thusly...

my ($second, $minute, $hour,
    $month_day, $month, $year,
    $weekday, $yearday, $isdst ) = localtime(time); 

...the phrase makes sense.  You are expanding the list of nine data
returned by localtime into convenient mnemonic variables.

(Getting further afield: I would probably never do THAT either -- if I
have a series of nine things that are tightly coupled, I would want
some better data structure to hold them.  A hash or hashref, would be
perfect.)

my $localtime; 
@$localtime{qw / second minute hour mday month year weekday yearday isdst /} = 
localtime(time);

[1] A style nit:

Write Perl, not C.

If you DO need to iterate across all the indices for an array ( rarely
necessary if you have designed your data correctly ) do it right:

my @person = ( qw / Freddy Mary Georgetta Loretta Fred Lawrence / ); 

##
## UNBEARABLY LAME C
##

for ( my $index = 0 ; $index < @person ; $index ++ ) {
    print "unbearably bad $person[$index]\n"; 
}

##
## MERELY BAD PERL
##

for my $index ( 0 .. $#person ) {
    print "merely bad $person[$index]\n"; 
}

##
## GETTING INTO THE PERL STATE OF MIND
##

for my $person (@person) {
    print "Hello $person\n";
}

Note that of the three techniques, the last is the only one that will
work for all values of $[ (of course, anyone who sets $[ to any
nonzero value deserves hot pokers in the eyes.)






-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to