Perl help

L

Lauren M. Bylsma

I'm trying to learn Perl to anaylze some data and I am having trouble
getting started, so if someone could help me out that would be greatly
appreciated. So I have a data file that looks something like this:


"INFORMATION"
"b2jun0302"
""
""
""
""
""

"SUMMARY"
"13" "Evt-" "correct" 1
"14" "Evt-" "incorrect" 1
"19" "Evt-" "rcor" 1
"20" "Evt-" "rincor" 1
"32" "Marker" "untitled"

"CHANNEL" "13"
"Evt-"
"No comment"
"correct"

3.92090
16.37657
19.26123
22.21257
25.54742

"CHANNEL" "14"
"Evt-"
"No comment"
"incorrect"

214.23345
238.11100
287.04997
379.90897
431.91602


What I want to do is take the numbers under Channel 13 and put them in an
array then Channel 14 in a separate array. Then I want to add another
column to each. For channel 13 I want this other column to contain all 1's.
For Channel 14 I want this column to contain all 0's. Then I want to put
them together
into one big array and sort it by number from smallest to largest.

Please help :)

-Lauren
 
T

Tad McClellan

Lauren M. Bylsma said:
Subject: Perl help


Please put the subject of your post in the Subject of your post.

Have you seen the Posting Guidelines that are posted here frequently?

What I want to do is take the numbers under Channel 13 and put them in an
array then Channel 14 in a separate array. Then I want to add another
column to each.


"column"?

What column?

I do not see any columns.

You would need a multi-dim array in order to have columns.

For channel 13 I want this other column to contain all 1's.
For Channel 14 I want this column to contain all 0's.


I'll need to ignore that part of the specification, as I don't
understand what it means...

Then I want to put
them together
into one big array and sort it by number from smallest to largest.


--------------------------------------
#!/usr/bin/perl
use strict;
use warnings;

my @combined;
{ local $/ = ''; # enable paragraph mode
while (<DATA>) {
next unless /^"CHANNEL" "(\d+)"/;
my @nums = split /\n/, scalar <DATA>; # read the next para
push @combined, @nums;
}
}

print "$_\n" for sort { $a <=> $b } @combined;

__DATA__
"INFORMATION"
"b2jun0302"
""
""
""
""
""

"SUMMARY"
"13" "Evt-" "correct" 1
"14" "Evt-" "incorrect" 1
"19" "Evt-" "rcor" 1
"20" "Evt-" "rincor" 1
"32" "Marker" "untitled"

"CHANNEL" "13"
"Evt-"
"No comment"
"correct"

3.92090
16.37657
19.26123
22.21257
25.54742

"CHANNEL" "14"
"Evt-"
"No comment"
"incorrect"

214.23345
238.11100
287.04997
379.90897
431.91602
 
L

Lauren M. Bylsma

I guess I was unclear, sorry I'm new to this. This is the output I would
want from the portion of the data file I provided earlier:

3.92090 1
16.37657 1
19.26123 1
22.21257 1
25.54742 1
214.23345 0
238.11100 0
287.04997 0
379.90897 0
431.91602 0

So I would want that stored into one array. I guess column isn't the
technical term but they sure look like columns to me.

-Lauren
 
T

Tad McClellan

[ Please do not post upside-down.
Please do not quote an _entire_ article.
Please do not quote .sigs.
Have you seen the Posting Guidelines that are posted here frequently?
]


Lauren M. Bylsma said:
This is the output I would
want from the portion of the data file I provided earlier:

3.92090 1
16.37657 1
19.26123 1
22.21257 1
25.54742 1
214.23345 0
238.11100 0
287.04997 0
379.90897 0
431.91602 0


my $extra = $1 == 13 ? '1' : '0'; # untested



push @combined, map "$_ $extra", @nums; # also untested

 
L

Lauren M. Bylsma

Thanks for your help but its not quite working. Here's what its giving me
as output:

Array 13:


Array 14:


FINAL:


So there's no numbers. Any idea what is wrong?

I changed the code slightly since its not going to be working on the same
data file each time, I'm going to put the data file name in the command line
when I run the script, so here's the slightly modified version of your
script that I used:

#!/usr/bin/perl -w

use diagnostics;


$/ = "";

while (<>)
{
if (index ($_, '"CHANNEL" "13"') > -1)
{ @Array13 = split (/\n/, (<>)); }
if (index ($_, '"CHANNEL" "14"') > -1)
{ @Array14 = split (/\n/, (<>)); last; }
}

$"= "\n";

print "Array 13:\n@Array13\n\nArray 14:\n@Array14\n\n";

push (@Final, map ("$_ 1", @Array13), map ("$_ 0", @Array14));

@Final = sort { $a <=> $b } @Final;

print "FINAL:\n@Final";



Thanks for your help,


-Lauren
 
L

Lauren M. Bylsma

I have a very good idea on what is wrong.


Umm well could you tell me what that would be?

-Lauren
 
L

Lauren M. Bylsma

Also, if you are implying that it doesn't work because of the minor change I
made, it also doesn't work when I use the code exactly as you provided.

-Lauren



"> > Thanks for your help but its not quite working.
 
L

Lauren M. Bylsma

It's not my computer's problem. I tried running the code on 2 different
unix servers, 1 of which I know has the latest edition of Perl. Guess you
can't admit your code might be screwed up, so you just insult me and my
computer. I don't understand why you guys have to be so mean about it, I
thought the point of forums like these is to help people. Not to mention
the insults I got earlier for not following the 20 pages of rules for this
newsgroup, sorry I didn't have time to read all of them. This was my first
time using this newsgroup and I sure won't be using it again. With all
those rules there should be something in there about not insulting people.

Lauren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top