"Casting" a split into an array

F

Fabrice Baro

my $seq = "ABC";
my %hash = ();
my $key = "key";

$hash{$key} = split "", $seq;

This gives the following warning: "Use of implicit split to @_ is
deprecated[...]"
I can get rid of the message by doing the following:

my @temp_array = split "", $seq;
$hash{$key} = @temp_array;

Is there a way to elegantly cast the result of my split into an array
in order to assign it directly in my hash ?
 
M

Mirco Wahab

Fabrice said:
my $seq = "ABC";
my %hash = ();
my $key = "key";

$hash{$key} = split "", $seq;

This gives the following warning: "Use of implicit split to @_ is
deprecated[...]"

You 'split' into scalar context. This is somehow unusual.
I can get rid of the message by doing the following:
my @temp_array = split "", $seq;
$hash{$key} = @temp_array;

Did you check what you got in $hash{$key}
Is there a way to elegantly cast the result of my split
into an array in order to assign it directly in my hash ?

You mean " assign it directly in my hash *element* "?
A hash element has to be a scalar. To express a list
(what split returns), give it a reference of the array
containing the list elements:

$hash{$key} = [ split '', $seq ];

to get the array back, dereference it:

$hash{$key} ===> reference (scalar)
@{ $hash{$key} } ===> array

Regards

M.
 
X

xhoster

Fabrice Baro said:
my $seq = "ABC";
my %hash = ();
my $key = "key";

$hash{$key} = split "", $seq;

This gives the following warning: "Use of implicit split to @_ is
deprecated[...]"

But the left hand side in parentheses:

( $hash{$key} ) = split "", $seq;

Xho
 
F

Fabrice Baro

Thank you all,

I wasn't that clear on what I wanted, sorry about that.
What I wanted was to split a string individually by character and
assign them to an array*; this array was to be assigned in the hash;
The solution that worked for me was Mirco's:
$hash{$key} = [ split '', $seq ];

xhos's suggestion:
( $hash{$key} ) = split "", $seq;
seems to assign only the first letter of $seq to the hash.

* hence the null splitting pattern ""; the reason I do this is to be
able to access each letter by index C++-style, e.g. $seq[0].
Is there a way to do this with perl ? The only indexing I found is
with substr(), but this function has little use for this case.
 
J

John W. Krahn

Fabrice said:
Thank you all,

I wasn't that clear on what I wanted, sorry about that.
What I wanted was to split a string individually by character and
assign them to an array*; this array was to be assigned in the hash;
The solution that worked for me was Mirco's:
$hash{$key} = [ split '', $seq ];

xhos's suggestion:
( $hash{$key} ) = split "", $seq;
seems to assign only the first letter of $seq to the hash.

* hence the null splitting pattern ""; the reason I do this is to be
able to access each letter by index C++-style, e.g. $seq[0].
Is there a way to do this with perl ? The only indexing I found is
with substr(), but this function has little use for this case.

What do you intend to do with the individual letters. There is probably a
better way to do it in Perl without using split.


John
 
P

Peter Wyzl

John W. Krahn said:
Fabrice said:
Thank you all,

I wasn't that clear on what I wanted, sorry about that.
What I wanted was to split a string individually by character and
assign them to an array*; this array was to be assigned in the hash;
The solution that worked for me was Mirco's:
$hash{$key} = [ split '', $seq ];

xhos's suggestion:
( $hash{$key} ) = split "", $seq;
seems to assign only the first letter of $seq to the hash.

* hence the null splitting pattern ""; the reason I do this is to be
able to access each letter by index C++-style, e.g. $seq[0].
Is there a way to do this with perl ? The only indexing I found is
with substr(), but this function has little use for this case.

What do you intend to do with the individual letters. There is probably a
better way to do it in Perl without using split.

Exactly..

I have previously seen a string split into characters and then compared
character by character with another string (as you would in C) rather than
just comparing the two strings (as you do in Perl).

This has the feel of an X-Y to me...

P
--
 
F

Fabrice Baro

the reason I do this is to be able to access each letter by index C++-style
I want to randomly select a certain number of letters among my
string.
I'm planning to shuffle an index and pick the desired number of
letters.

Original:
0 1 2 3 4 5 <- index
L G R D T I <- letters

Shuffle:
4 2 5 1 0 3
T R I G
^
Pick until here

Does anyone see a better way ?
 
M

Mirco Wahab

Fabrice said:
I'm planning to shuffle an index and pick the desired number of
letters.

Original:
0 1 2 3 4 5 <- index
L G R D T I <- letters

Shuffle:
4 2 5 1 0 3
T R I G
^
Pick until here

Does anyone see a better way ?

I tried it but could not
come up with a very clever solution ;-)

Thats what I have:



use strict;
use warnings;
use List::Util qw'shuffle';

sub CODES { my $z=1; join '', map substr($_[0],$_,1), grep $z*=$_,@_[1..$#_] }

my @index = qw'0 1 2 3 4 5';
my $string ='LGRDTI';

my $sh = CODES $string, shuffle @index;

print $sh;


Regards

M.
 
J

John W. Krahn

Fabrice said:
I want to randomly select a certain number of letters among my
string.
I'm planning to shuffle an index and pick the desired number of
letters.

Original:
0 1 2 3 4 5 <- index
L G R D T I <- letters

Shuffle:
4 2 5 1 0 3
T R I G
^
Pick until here

Does anyone see a better way ?

Need ... more ... context. :)

A lot depends on where and how often the code will be executed.



John
 
P

Peter Wyzl

Fabrice Baro said:
I want to randomly select a certain number of letters among my
string.
I'm planning to shuffle an index and pick the desired number of
letters.

Original:
0 1 2 3 4 5 <- index
L G R D T I <- letters

Shuffle:
4 2 5 1 0 3
T R I G
^
Pick until here

Does anyone see a better way ?

Fabrice Baro said:
I want to randomly select a certain number of letters among my
string.
I'm planning to shuffle an index and pick the desired number of
letters.

Original:
0 1 2 3 4 5 <- index
L G R D T I <- letters

Shuffle:
4 2 5 1 0 3
T R I G
^
Pick until here

What is the condition that determines where 'here' is? Is it the same each
time or is it different based on some condition? (i.e. until a specific
letter is found? Or x number of letters?)

Why build an array, and an array of the indexes when you can just shuffle
the array and then step through the indexes until you meet the above
condition? Is there some reason the array of letters needs to preserve it's
order (which can't be met by preserving the pre-split sting?)?

Shuffling an array is a FAQ.... So maybe your answers to the two above
questions will help you a little...

Modified from PerlFAQ4

my $string = 'abcdefghijklmnopqrstuvwxyz';
my @letters = split //, $string;
fisher_yates_shuffle( \@letters ); # randomize @letters in place
print @letters;


sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to the array
my $i = @$deck;
while ($i--) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}


In this array the indexes remain sequential and any letter can be accessed
directly by $letters[$arrayindex]...



I hope I'm not completely on the wrong track...

P

--
 
F

Fabrice Baro

Thanks Peter,
What is the condition that determines where 'here' is? Is it the same each
time or is it different based on some condition? (i.e. until a specific
letter is found? Or x number of letters?)

The condition is variable and is x number of letters.
Why build an array, and an array of the indexes when you can just shuffle
the array and then step through the indexes until you meet the above
condition? Is there some reason the array of letters needs to preserve it's
order (which can't be met by preserving the pre-split sting?)?

I don't need to preserve the order of the letters, but I need to
shuffle many strings in the exact same way.
[more... context ;P John] In fact, I'm downsizing alignments of genes
for simulation purposes. So the order of the letters doesn't matter as
much as to pick the same positions for each sequence.
On my previous example, I showed the indexes only for clarity
purposes. But in my particular case, I think I don't have another
option than to build an actual array of indexes to be able to apply
the same to more than an array.
As for the shuffling itself, why not use the 'shuffle' function from
List::Util ? I think it can get the job done.
 
F

Fabrice Baro

Thanks Peter,
What is the condition that determines where 'here' is? Is it the same each
time or is it different based on some condition? (i.e. until a specific
letter is found? Or x number of letters?)

The condition is variable and is x number of letters.
Why build an array, and an array of the indexes when you can just shuffle
the array and then step through the indexes until you meet the above
condition? Is there some reason the array of letters needs to preserve it's
order (which can't be met by preserving the pre-split sting?)?

I don't need to preserve the order of the letters, but I need to
shuffle many strings in the exact same way.
[more... context ;P John] In fact, I'm downsizing alignments of genes
for simulation purposes. So the order of the letters doesn't matter as
much as to pick the same positions for each sequence.
On my previous example, I showed the indexes only for clarity
purposes. But in my particular case, I think I don't have another
option than to build an actual array of indexes to be able to apply
the same to more than an array.
As for the shuffling itself, why not use the 'shuffle' function from
List::Util ? I think it can get the job done.
 
M

Mirco Wahab

Fabrice said:
[more... context ;P John] In fact, I'm downsizing alignments of genes
for simulation purposes. So the order of the letters doesn't matter as
much as to pick the same positions for each sequence.
On my previous example, I showed the indexes only for clarity
purposes. But in my particular case, I think I don't have another
option than to build an actual array of indexes to be able to apply
the same to more than an array.

Can you give one example of what you *really*
want to do? With some exact input data and some
output which you would expect from it?

Regards

Mirco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top