CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
help please!

 



beginner
Novice

Apr 22, 2009, 11:51 AM

Post #1 of 21 (2141 views)
help please! Can't Post

Hi

I'm very new to perl and very confused. I'm trying to summarize data from a file into one tab delimited table. I have many different colours, and objects of those colours. The data file contains lines with lots of random stuff on, but also things like "blue_ball blue_bat red_ball red_bat green_ball green_bat". If something like blue_ball is repeated i don't want to include the repeat.

What I want in the end is a file which can open in something like open office spreadsheet which has:

Colour blue orange green red purple

blue_ball;blue_bat none green_ball;green_bat red_bat;red_ball none

I think what i want is foreach loops and an array, but not really sure. Any pointers would be amazing

Thanks very much


1arryb
User

Apr 22, 2009, 12:43 PM

Post #2 of 21 (2137 views)
Re: [beginner] help please! [In reply to] Can't Post

Hi beginner,

Please provide a sample of your input.
For output, any spreadsheet program can import a text file with comma or tab-delimited fields.

Cheers,

Larry


beginner
Novice

Apr 22, 2009, 2:26 PM

Post #3 of 21 (2131 views)
Re: [1arryb] help please! [In reply to] Can't Post

Thanks Larry!

Here's a sample file (I have many items so want each item to be a new line in the table). Most are a lot longer so want something that works with a different number of lines.

#

item 1 list
new data;
1 red_bat,
2 blue_bat,
3 red_car,
4 red_ball,
5 green_bat,
6 orange_ball,
7 blue_car,
8 orange_ball,
9 purple_bat,
10 red_car,
11 purple_ball,
12 purple_ball,
13 red_ball,
14 green_ball,
15 orange_ball,
16 purple_ball,
17 red_car,
18 purple_bat,
19 orange_
20 blue_bat,
21 red_ball,
22 orange_car,
23 blue_bat,
24 purple_ball,
25 green_car,
26 orange_car,
27 green_bat,
end list;

#

Thanks again!


1arryb
User

Apr 22, 2009, 2:49 PM

Post #4 of 21 (2128 views)
Re: [beginner] help please! [In reply to] Can't Post

beginner,

Given this input, what would the output be? Is this one record or many? In either case, how do I tell where a new record begins?

Larry


beginner
Novice

Apr 22, 2009, 3:52 PM

Post #5 of 21 (2127 views)
Re: [1arryb] help please! [In reply to] Can't Post

Hi Larry

The output would be:

colour red blue green orange purple
item 1 red_bat;red_ball;red_car blue_bat;blue_car;blue_ball green_ball;green_bat;green_car orange_ball;orange_bat;orange_car purple_car;purple_bat;purple_ball

The next line "item 2" is in the same format from a different file (the next file in the folder). I think i want to be calling an array, with the keys being the colours and the values being the colour_object, but i'm not sure how to add additional objects to the values or how to do it really.

Thanks again.


KevinR
Veteran


Apr 22, 2009, 4:00 PM

Post #6 of 21 (2124 views)
Re: [beginner] help please! [In reply to] Can't Post

Beginner,

what have you tried so far to solve your requirments? Since the input and output are not something that looks like anything useful or practical, I assume this is school work or similar.
-------------------------------------------------


beginner
Novice

Apr 22, 2009, 4:26 PM

Post #7 of 21 (2120 views)
Re: [KevinR] help please! [In reply to] Can't Post

Hi KevinR

I've read a little about perl and i just want to try a few things out to see how it works compared to matlab.

What i tried so far is this (really doesn't work though but think it may be on the track o gettingrid of the dupilcates?)

@Files=`ls *.items`; chomp(@Files);
foreach $f (@Files) {
open (IN, $f) or die "Can't open $infile\n";
@items = <IN>;
close (IN);
my %object = ();
my @unique = grep { ! $object{ $_ }++ } @items;
open(OUT, ">tmp1");
close(OUT);
}

I'm really looking for examples so i can understand how the different operations work, and thought the example given requires specificity, repeats, and outputs.

Thanks for any help.


KevinR
Veteran


Apr 22, 2009, 4:43 PM

Post #8 of 21 (2118 views)
Re: [beginner] help please! [In reply to] Can't Post

print the contents of @unique to the screen or a file to test the ouptut.
-------------------------------------------------


beginner
Novice

Apr 23, 2009, 1:12 AM

Post #9 of 21 (2101 views)
Re: [KevinR] help please! [In reply to] Can't Post

Hi KevinR

When i execute the following:

use Getopt::Std;
use File::Copy;
@Files=`ls *.items`; chomp(@Files);
foreach $f (@Files) {
open (IN, $f) or die "Can't open $infile\n";
@items = <IN>;
close (IN);
my %object = ();
my @unique = grep { ! $object{ $_ }++ } @items;
open(OUT, ">tmp1");
close(OUT);
print @unique;
}
exit


I get the following output

malloc: ../bash/dispose_cmd.c:241: assertion botched
free: called with unallocated block argument
Aborting...Aborted

?

Thanks


FishMonger
Veteran / Moderator

Apr 23, 2009, 4:42 AM

Post #10 of 21 (2094 views)
Re: [beginner] help please! [In reply to] Can't Post

That error is not coming from Perl, it's coming from your shell.

Have you tried doing a google search on that error?
http://www.google.com/search?q=malloc%3A+..%2Fbash%2Fdispose_cmd.c%3A241%3A+assertion+botched&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a


beginner
Novice

Apr 23, 2009, 6:54 AM

Post #11 of 21 (2088 views)
Re: [FishMonger] help please! [In reply to] Can't Post

Thanks FishMonger

I added

#!/usr/bin/perl

to the top of the script and it runs ok now. Unfort when i print out it just gives:


item 1 list
new data;
1 red_bat,
2 blue_bat,
3 red_car,
4 red_ball,
5 green_bat,
6 orange_ball,
7 blue_car,
8 orange_ball,
9 purple_bat,
10 red_car,
11 purple_ball,
12 purple_ball,
13 red_ball,
14 green_ball,
15 orange_ball,
16 purple_ball,
17 red_car,
18 purple_bat,
19 orange_
20 blue_bat,
21 red_ball,
22 orange_car,
23 blue_bat,
24 purple_ball,
25 green_car,
26 orange_car,
27 green_bat,
end list;


and the file tmp1 is empty... Obviously doing soemthing major wrong...

Thanks


FishMonger
Veteran / Moderator

Apr 23, 2009, 7:27 AM

Post #12 of 21 (2085 views)
Re: [beginner] help please! [In reply to] Can't Post

Here's a list of things you're doing wrong.

1) You're not using the strict and warnings pragmas

2) You're shelling out to get the list of files instead of using one of Perl's built-in methods.

3) The die statement on the open call is not including the reason it failed and you fail to do the same test on the second open call.

4) You're declaring my %object = (); inside the loop.

5) Upon opening the OUT filehandle, you immediately close it without writing any data to it.

There are several other issues, but fix the ones I mentioned and then we'll address the others.


beginner
Novice

Apr 23, 2009, 8:28 AM

Post #13 of 21 (2079 views)
Re: [FishMonger] help please! [In reply to] Can't Post

Thanks FishMonger

I've tried fixing those problems, but coming up against a bad dirhandle warning which I'm not sure how to fix?

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use File::Copy;
my $DIR="/home/test";
my @Files = grep(/\.items$/,readdir($DIR));
my %object = ();
foreach my $f (@Files) {
open (IN, $f) or die "can't open infile\n";
my @items = <IN>;
close (IN);
my @unique = grep { ! $object{ $_ }++ } @items;
open(OUT, ">tmp1");
print(OUT @unique);
close(OUT);
print @unique;
}
exit


Bad symbol for dirhandle at /home/test.pl line 7. If i remove use strict it works so not rally sure what it is.



Thanks again for all the help!


FishMonger
Veteran / Moderator

Apr 23, 2009, 8:43 AM

Post #14 of 21 (2077 views)
Re: [beginner] help please! [In reply to] Can't Post

In order to use readdir, you first need to use opendir to create the directory handle.


Code
my $dir = "/home/test"; 
open my $DIR_HANDLE, $dir or die "failed to create directory handle to '$dir' $!";

my @Files = grep(/\.items$/, readdir($DIR_HANDLE));

Please use the code tags, like I've done, when posint code blocks.


FishMonger
Veteran / Moderator

Apr 23, 2009, 8:45 AM

Post #15 of 21 (2075 views)
Re: [FishMonger] help please! [In reply to] Can't Post


In Reply To
In order to use readdir, you first need to use opendir to create the directory handle.


Code
my $dir = "/home/test"; 
open my $DIR_HANDLE, $dir or die "failed to create directory handle to '$dir' $!";

my @Files = grep(/\.items$/, readdir($DIR_HANDLE));

Please use the code tags, like I've done, when posint code blocks.


Here's the short hand version.

Code
my @Files = <$dir/*.items>;



beginner
Novice

Apr 23, 2009, 9:23 AM

Post #16 of 21 (2072 views)
Re: [FishMonger] help please! [In reply to] Can't Post

When I use that i still get the warning " failed to create directory handle to '~/test/' No such file or directory at ./test2.pl line 7"

<c>

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use File::Copy;
my $dir = "~/test/";
open my $DIR_HANDLE, $dir or die "failed to create directory handle to '$dir' $!";
my @Files = grep(/\.items$/, readdir($DIR_HANDLE));
my %object = ();
foreach my $f (@Files) {
open (IN, $f) or die "can't open infile\n";
my @items = <IN>;
close (IN);
my @unique = grep { ! $object{ $_ }++ } @items;
open(OUT, ">tmp1");
print(OUT @unique);
close(OUT);
print @unique;
}
exit;

<c>

Thanks


beginner
Novice

Apr 23, 2009, 9:24 AM

Post #17 of 21 (2071 views)
Re: [FishMonger] help please! [In reply to] Can't Post

Sorry thought did use the code things but not too sure how to make it work!


FishMonger
Veteran / Moderator

Apr 23, 2009, 10:34 AM

Post #18 of 21 (2067 views)
Re: [beginner] help please! [In reply to] Can't Post

This will fix 1 problem.

Code
my $dir = "/home/test"; 
my @Files = <$dir/*.items>;



beginner
Novice

Apr 23, 2009, 11:48 AM

Post #19 of 21 (2062 views)
Re: [FishMonger] help please! [In reply to] Can't Post

Cool! So

Code
#!/usr/bin/perl 
use strict;
use warnings;
use Getopt::Std;
use File::Copy;
my $dir = "/home/test";
my @Files = <$dir/*.items>;
my %object = ();
foreach my $f (@Files) {
open (IN, $f) or die "can't open infile\n";
my @items = <IN>;
close (IN);
my @unique = grep { ! $object{ $_ }++ } @items;
open(OUT, ">tmp1");
print(OUT @unique);
close(OUT);
print @unique;
}
exit

I don't get any errors, but it doesn't seem to do anything though?
Thanks FishMonger!


FishMonger
Veteran / Moderator

Apr 23, 2009, 12:01 PM

Post #20 of 21 (2059 views)
Re: [beginner] help please! [In reply to] Can't Post

Are the .items files in "/home/test"?

Add some print statements after each var assignment to see if the var actually holds what you think it should.


beginner
Novice

Apr 23, 2009, 12:20 PM

Post #21 of 21 (2057 views)
Re: [FishMonger] help please! [In reply to] Can't Post

So i put in the prints

Code
#!/usr/bin/perl 
use strict;
use warnings;
use Getopt::Std;
use File::Copy;
my $dir = "/home/test";
my @Files = <$dir/*.items>;
my %object = ();
foreach my $f (@Files) {
open (IN, $f) or die "can't open infile\n";
my @items = <IN>;
print @items;
close (IN);
my @unique = grep { ! $object{ $_ }++ } @items;
open(OUT, ">tmp1");
print @unique;
print(OUT @unique);
close(OUT);
print @unique;
}
exit


And it gives the following output:


Quote
item 1 list
new data;
1 red_bat,
2 blue_bat,
3 red_car,
4 red_ball,
5 green_bat,
6 orange_ball,
7 blue_car,
8 orange_ball,
9 purple_bat,
10 red_car,
11 purple_ball,
12 purple_ball,
13 red_ball,
14 green_ball,
15 orange_ball,
16 purple_ball,
17 red_car,
18 purple_bat,
19 orange_
20 blue_bat,
21 red_ball,
22 orange_car,
23 blue_bat,
24 purple_ball,
25 green_car,
26 orange_car,
27 green_bat,
end list;
item 1 list
new data;
1 red_bat,
2 blue_bat,
3 red_car,
4 red_ball,
5 green_bat,
6 orange_ball,
7 blue_car,
8 orange_ball,
9 purple_bat,
10 red_car,
11 purple_ball,
12 purple_ball,
13 red_ball,
14 green_ball,
15 orange_ball,
16 purple_ball,
17 red_car,
18 purple_bat,
19 orange_
20 blue_bat,
21 red_ball,
22 orange_car,
23 blue_bat,
24 purple_ball,
25 green_car,
26 orange_car,
27 green_bat,
end list;
item 1 list
new data;
1 red_bat,
2 blue_bat,
3 red_car,
4 red_ball,
5 green_bat,
6 orange_ball,
7 blue_car,
8 orange_ball,
9 purple_bat,
10 red_car,
11 purple_ball,
12 purple_ball,
13 red_ball,
14 green_ball,
15 orange_ball,
16 purple_ball,
17 red_car,
18 purple_bat,
19 orange_
20 blue_bat,
21 red_ball,
22 orange_car,
23 blue_bat,
24 purple_ball,
25 green_car,
26 orange_car,
27 green_bat,
end list;

It seems to be just taking in the whole file and and choosing duplicates. I'm not to sure how to select just for the colour_object?
Thanks again!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives