Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array instance in another array

Status
Not open for further replies.

alanchia

Technical User
Jan 8, 2003
42
SG
hi all,

can i do this ?

@testing=<MYFILES>;
@contents = qw($testing[5]);

if not how do i go about doing that ?

thanks
 
You can't do that because qw() doesn't do variable interpolation. So you would would get the literal string '$testing[5]' and not the 6th element of the @testing array. Just get rid of the qw.
Code:
@contents = ( $testing[5] );
# Or even
push @contents, $testing[5];
jaa
 
thanks,

but what if my testing[5] is a sentence with whitespaces in between and i want each word to be an instance of @contents ? e.g

print $testing[5]; would give &quot; i am a boy&quot;

and i want

print contents[0]; to give &quot; i &quot;
print contents[1]; to give &quot; am &quot;
and so forth..

thanks
alan
 
Split only defaults to whitespace when it defaults to $_. i.e. you can only leave off the pattern if you are splitting $_ and split has no other arguments, otherwise you have to include the pattern.
Code:
$_ = 'i am a boy';
@contents = split;
# or
@contents = split /\s+/, $_;
# or even
@contents = split /\s+/;
# but not
@contents = split $_;
The pattern must be the first or only argument.
So the pattern is necessary in the above example.
Code:
my @contents = split /\s+/, $testing[5];
jaa
 
Thanks alot ppl, appreciate your help..

cheers
alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top