Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi everyone, I am trying a perl code that iterates over an array ( this array is list files collected from various servers) and assigns them to an hash after a split. The problem I have is when I iterate over a large array the split assigns wrong values to the parameters for some reason. Here is a sample code.
@bitter = `ssh serverlist 'ls -ltr'` foreach (@bitter) { my($first,$second) = split(/=/); $bright->{$variable}{$first} = $second; $printer{$variable} = 1; }
Some how for large arrays the split seems to assign wrong values to $first and $second. Any suggestions please?

Replies are listed 'Best First'.
Re: wrong value assigned
by thezip (Vicar) on May 01, 2007 at 21:54 UTC

    It seems that you might have a faulty assumption about the data.

    Try using Data::Dumper to double-check that your data is as you think it is:

    use strict; use warnings; use Data::Dumper; my @bitter = `ssh serverlist 'ls -ltr'` print Dumper(\@bitter); ...

    I also notice that you aren't using any strictures... why not?


    Where do you want *them* to go today?
Re: wrong value assigned
by GrandFather (Saint) on May 01, 2007 at 21:55 UTC

    How about showing us a small sample of the data that fails? Use the following code as a template and show us the output you get and what you expected.

    use strict; use warnings; my @bitter = ('x=y', 'z=p=q'); foreach (@bitter) { my ($first, $second) = split /=/; print ">$first< [$second]\n"; }

    Prints:

    >x< [y] >z< [p]

    DWIM is Perl's answer to Gödel
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: wrong value assigned
by friedo (Prior) on May 01, 2007 at 21:50 UTC
    What are the "wrong" values being assigned? You're probably going to have to take a look at your data to see why it's failing.