in reply to Re: Re: Re: going loopy
in thread going loopy

Sorry broquaint I didn't mean to sound rude! ;-) This problem is with the split function which I often use (but it normally works).

From my earlier post, I had an array named @unique_slopes which if printed to the screen looks like this;

0.1375 0.102999999999999998 0.132500000000000002 0.264333333333333331 +0.636000000000000009 0.537499999999999989

I wish to be able to access each element individually and thought that using 'join' followed by 'split' would allow me to do this;

$unique_slopes = join ('', @unique_slopes); @unique_slopes = split (/\s+/, $unique_slopes);
Split appears to be returning the original string as a single string accessible by $unique_slopes[[0]], which is not what I wanted.

Do you know of an alternative way to split the array so that I can access individual elements? A whitespace character appears to be present between each number.

Thanks for your time.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: going loopy
by broquaint (Abbot) on May 14, 2003 at 14:30 UTC
    I wish to be able to access each element individually and thought that using 'join' followed by 'split' would allow me to do this;
    Erm, I think you need to read perldata, which should inform you about how arrays work in perl.

    What you're doing with your code is joining an array on an empty string, which will create one string comprised of the elements of the array joined together. Then you're splitting on any whitespace1 in that string, which won't work since your array won't contain any whitespace, then split returns the original string which is then assigned to $uniques_slopes[0].

    As you can guess, this does not do what you want at all. To access individual array elements, you just access them using the square bracket syntax like so

    ## see. perlop for more info on qw// my @array = qw/ one two three four five /; ## create a range from 0 to the index of the last element in @array for my $i (0 .. $#array) { ## acccessing element at $i using square bracket syntax print $array[$i]; } ## this is a more idiomatic approach print for @array;
    Thanks for the info, it has made helping much easier, and if you plan on sticking around then I recommend joining up :)
    HTH

    _________
    broquaint

    1 see. the split docs on why you can just a ' ' as the delimiter for this special case