homepage Welcome to WebmasterWorld Guest from 23.22.179.210
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
join('')
medranoenrique




msg:3707569
 7:13 pm on Jul 25, 2008 (gmt 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]

 

phranque




msg:3707746
 10:19 pm on Jul 25, 2008 (gmt 0)

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).

perl_diver




msg:3707811
 11:48 pm on Jul 25, 2008 (gmt 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.

perl_diver




msg:3707816
 11:55 pm on Jul 25, 2008 (gmt 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]

phranque




msg:3707836
 12:21 am on Jul 26, 2008 (gmt 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.

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";

perl_diver




msg:3707954
 5:35 am on Jul 26, 2008 (gmt 0)

Yes, but that is an unusual place to check for an error. But for debugging purposes it could be helpful.

medranoenrique




msg:3709262
 2:55 pm on Jul 28, 2008 (gmt 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

perl_diver




msg:3709438
 5:17 pm on Jul 28, 2008 (gmt 0)

open (GOOD, '>', 'names') or die "$!"; # open for creating/over-writing
open (GOOD, '>>', 'names') or die "$!"; # open for creating/appending

medranoenrique




msg:3710499
 6:09 pm on Jul 29, 2008 (gmt 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.

phranque




msg:3710594
 8:03 pm on Jul 29, 2008 (gmt 0)

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

perl_diver




msg:3710705
 10:44 pm on Jul 29, 2008 (gmt 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]

medranoenrique




msg:3711325
 4:43 pm on Jul 30, 2008 (gmt 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]

perl_diver




msg:3711341
 5:08 pm on Jul 30, 2008 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved