in reply to Re: Arrays not being manipulated
in thread Arrays not being manipulated

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 :/

Replies are listed 'Best First'.
Re^3: Arrays not being manipulated
by William G. Davis (Friar) on Jan 01, 2005 at 08:47 UTC

    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