in reply to Arrays not being manipulated

Beyond what was already mentioned:

@numberMutation = split(/\C/,$_);

Are you sure you don't mean to use push() there instead of assignment? Assignment assigns a new list to an array (that is, it replaces the current list with another):

@array = qw(one two three); # @array contains "one", "two" and "three" @array = qw(four five); # @array now contains just "four" and "five"

push() adds scalars to the end of an array, like this:

@array = qw(one two three); # @array now contains "one", "two" and "three" push(@array, qw(four five)); # @array now contains "one", "two", "three", "four" and "five"

Also, why are you splitting $_ on \C? The \C metasymbol means match one byte exactly, not the capital letter "C" or a carriage return or anything else. If you want to match "C", the letter, then just get rid of the backslash. If you want to match a carriage return, then binmode() the file handle before reading from it and use \015 instead of \C.

Replies are listed 'Best First'.
Re^2: Arrays not being manipulated
by aristotle73 (Sexton) on Jan 01, 2005 at 08:20 UTC
    Because, I plan to write another perl script that will calculate pi and write it to a file. The current file being used, pi.txt, has the following formating that will eventually match the other script: 31415926535897932384626433832 So I need the string to be broken down into single values (like 3 or just 1 or just 4) in order to do the comparisons. If I don't declare all those variables as being global, Perl refuses to compile the script :/

      Regardless, split(/\C/, $_) is still wrong. You want split(//, $_) instead. That will split "314" up into "3", "1", and "4" like you want.

      Also, you don't need to declare them as global with our. Declare varaibles with my instead, when you first use them:

      #!/usr/bin/perl -w #filename: myMutator.pl use strict; my $file = shift || "myFiles/pi.txt"; open(FILE, "< $file") or die "Couldn't open $file: $!"; my @numberMutation; while (<FILE>) { chomp; push(@numberMutation, split(//, $_)); } my @organism = (1,1,1,1,1,1,1,1,1); foreach my $currentMutation (@numberMutation) { my $i = 0; foreach my $currentGene (@organism) { if ($currentGene < $currentMutation) { $organism[$i] = $currentMutation; $i++; } } } foreach my $someGene (@organism) { print "$someGene\n"; } foreach my $thisMutate (@numberMutation) { print "$thisMutate\n"; }

      I'm still not quite sure what you're trying to do, but if the file only contains a single line, you can avoid using a loop to read from it and just do this:

      chomp(my $line = <FILE>); my @numberMutation = split(//, $line);
        To follow up, here's some code indicating what split /\C/ does:
        #!perl -w use strict; while (<DATA>) { chomp; warn "input line is '$_'\n"; my @t = split /\C/, $_; warn "split to [n=@{[ scalar @t ]}]: '@t'\n"; } # input line is '31415926' # split to [n=0]: '' # input line is '12345678' # split to [n=0]: '' __DATA__ 31415926 12345678