Welcome to WebmasterWorld Guest from 54.162.164.247

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

join('')

     
7:13 pm on Jul 25, 2008 (gmt 0)

New User

5+ Year Member

joined:July 17, 2008
posts:30
votes: 0


can any one explain to me about the join function and how to use it

what im trying to do is open a file and use the join function to join them togeter and then use that to ovverright the file names.

the file names just has

Jacob
Michael
Joshua
Matthew

this is what i have so far
#!/usr/bin/perl -w

open (GOOD, '<names');
@myNames = (<GOOD>);
#print "@myNames\n";
#exit;
$hio = join(',', @myNames);
print "$hio";
print(<GOOD>) "$hio" or die "couldnt print and join";

close (GOOD);

the problem is in the line with
print(<GOOD>) "$hio" or die "couldnt print and join";

[edited by: phranque at 10:11 pm (utc) on July 25, 2008]
[edit reason] disabled smileys ;) [/edit]

10:19 pm on July 25, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10887
votes: 72


try this:
print(GOOD) "$hio" or die "couldnt print and join";

the print statement takes a file handle, which does not include the "<>".
the angle brackets are an i/o operator that returns the next line of that file(handle).

11:48 pm on July 25, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


oops, that will not work. Should be:

print GOOD "here is what you want to print" or die "blah blah blah";

The parenthesis will throw a syntax error.

11:55 pm on July 25, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


Lets take a closer look at this code:


this is what i have so far
#!/usr/bin/perl -w

open (GOOD, '<names'); <-- FILE OPENED FOR READING ONLY

@myNames = (<GOOD>); <-- READS TO THE END OF THE FILE AND STORES ALL THE LINES IN THE ARRAY. THE PARENTHESIS ARE NOT NEEDED.

#print "@myNames\n";
#exit;

$hio = join(',', @myNames); <-- JOINS ALL THE LINES WITH A COMMA BUT THERE ARE STILL NEWLINES IN THE LINES SO THE OUTPUT WILL LOOK AKWARD

print "$hio"; <-- PRINTS THE JOINED LINES

print(<GOOD>) "$hio" or die "couldnt print and join"; <-- THIS DOES NOTHING BECAUSE THE FILE POINTER IS AT THE END OF THE FILE SO THERE IS NOTHING LEFT TO READ. IT DOES NOT PRINT TO THE FILE WHICH WAS OPENED FOR READING ONLY ANYWAY.

close (GOOD);

[edited by: phranque at 12:16 am (utc) on July 26, 2008]
[edit reason] disabled smileys ;) [/edit]

12:21 am on July 26, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10887
votes: 72


oops, that will not work. Should be:

print GOOD "here is what you want to print" or die "blah blah blah";

The parenthesis will throw a syntax error.

good catch, perl_diver!
it probably wouldn't hurt to put the error number in the error message:
print GOOD "here is what you want to print" or die "$!: blah blah blah";

5:35 am on July 26, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


Yes, but that is an unusual place to check for an error. But for debugging purposes it could be helpful.
2:55 pm on July 28, 2008 (gmt 0)

New User

5+ Year Member

joined:July 17, 2008
posts:30
votes: 0


now i know what my problem is

open (GOOD, '<names'); <-- FILE OPENED FOR READING ONLY

i want to open a file for printing
thanks perl_diver

so how would i do that

5:17 pm on July 28, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


open (GOOD, '>', 'names') or die "$!"; # open for creating/over-writing
open (GOOD, '>>', 'names') or die "$!"; # open for creating/appending
6:09 pm on July 29, 2008 (gmt 0)

New User

5+ Year Member

joined:July 17, 2008
posts:30
votes: 0


the problem that i'm really having may not be sovled if i use the join function what i am trying to do is the following

lets say i have this in a .txt file
>YCR008W SAT4 SGDID:S000000601, Verified ORF
ATGACTGGTATGAATGATAATAATGCCGCTATTCCTCAGCAAACTCCAAGGAAA
>YCR090C SGDID:S000000686, Uncharacterized ORF
ATGCCGTTATTTTTGGTTCTGAAAGCAACATTATCAGAAAACGT
>YCR008W SAT4D SGDID:dupe1, Verified ORF
ATGACTGGTATGAATGATAATAATGCCGCTAT

what im trying to do is find a way to get the lines that end in Verified ORF and the line after it

sorry for wasting your time with the join function.

8:03 pm on July 29, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10887
votes: 72


this might work:
- read the text file into a scalar string
- split the scalar into an array on newlines
- rejoin the array into a scalar string
- resplit the new scalar string on '>'
- use regular expressions to find the array elements containing "Verified ORF" and delete the others
10:44 pm on July 29, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


Seems you could just read the file and look for "Verified ORF" on the end then do another readline to get the next line:



while (my $line = <FILE>) {
chomp $line;
if ($line =~ /Verified ORF$/) {
my $next_line = <FILE>;
push @lines, "$line $next_line";
}
}

Or some variation thereof.

[edited by: phranque at 11:11 pm (utc) on July 29, 2008]
[edit reason] disabled smileys ;) [/edit]

4:43 pm on July 30, 2008 (gmt 0)

New User

5+ Year Member

joined:July 17, 2008
posts:30
votes: 0


thanks perl_diver it worked

while (my $line = <FILE>) {
chomp $line;
if ($line =~ /Verified ORF$/) {
my $next_line = <FILE>;
push @lines, "$line $next_line";
}
}

[edited by: phranque at 9:51 pm (utc) on July 30, 2008]
[edit reason] disabled smileys ;) [/edit]

5:08 pm on July 30, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


Even with a smiley in there it worked? You're welcome.