perl -write values in a file to @array in perl

#!/usr/bin/perl -w
open(FILE, ">", "dataFile");
while(<FILE>) {
chomp;
if ($_ =~ /^[[:alpha:]]+$/) {push @stringArray, $; }
elsif ($
=~ /^\d+$/) {push @intArray, $_;}
}
close(FILE);
$lengthArray=@intArray;
for($i=0; $i<$lengthArray; $i++) {print"\n$intArray[$i] $stringArray[$i]";}
print("\n");
exit 0

Using the code you provided I was able to see what needed to be done.. Thank you. However, I have a small question. To pull in the alpha char I was unable to use \w and went with what you had [[:alpha:]] otherwise I would pull in all entries.

So here is the question... what is the difference between \w and :alpha:

/r
Rick

By running the code you can see there is a difference between the two.

\w is the same as [:word:] which is the same as: a-zA-Z0-9_
[:alpha:] matches only letters a-zA-Z for which perl has no equivalent shortcut character class.

We can trap colums with alpha [:alpha;], numeric \d... So how can we trap colums with alphanum without catching everything?

if ($_ =~ /^[[:alpha:]]+$/) {
push @stringArray, $;
} elsif ($
=~ /^\d+$/) {
push @intArray, $;
} elsif ($
=~ /^??????+$/) {
push @alphnumArray, $_;

/r
Rick

With the [:alnum:] POSIX character class.

tyler_durden

@arr=<DATA>;
map {s/\n//} @arr;
print join ",", @arr;
__DATA__
19
20
21
22
40 

So if below works on a single column datafile of
Apple
B34
Cat
112
245
356

Outputs to screen:
112 Apple
245 B34
356 Cat

open(FILE, "<", "dataFile");
while(<FILE>) {
chomp;
if ($_ =~ /^[[:digit:]]+$/) {
push @intArray, $;
} elsif (($
=~ /^[[:alpha:]]+$/)||($_ =~ /^[[:alnum:]]+$/)) {
push @stringArray, $_;
}
}
close(FILE);
$lengthArray=@intArray;
for($i=0; $i<$lengthArray; $i++) {
print"\n$intArray[$i] $stringArray[$i] ";
}

How can I take a datafile that has a special character and have mult arrays. First array catches everything before the special character �$� and the second array catches everything after the special character?

Abc is good for me $4.55
Def might not be enough $5.66
Ghi price to play $6.77

/r
Rick

$
$ cat data.txt
Abc is good for me $4.55
Def might not be enough $5.66
Ghi price to play $6.77
$
$ perl -ne 'chomp; split /\$/; push @a1,$_[0]; push @a2,$_[1]; END{foreach $i(@a1){print $i,"\n"} foreach $i(@a2){print $i,"\n"}}' data.txt
Abc is good for me
Def might not be enough
Ghi price to play
4.55
5.66
6.77
$
$

tyler_durden

In script form:


my @before;
my @after;
open(FILE, "<", "dataFile");
while(<FILE>) {
   chomp;
   my ($before, $after) = split(/\$/);
   push @before, $before;
   push @after, $after;
}

for my $i (0 .. $#before) {
   print "$before[$i] $after[$i]\n";
}

Assumes all lines have the '$' character in them, if not, adjust accordingly.