Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatenation in perl

Status
Not open for further replies.

gammaman1

Programmer
Jun 30, 2005
23
US
hi,

i'm having trouble with concatenation in perl. What I need to do is put the input of a file into an array. Then I want to join certain indices in the array. For example:

open FILENAME, "testtest";
@mylist = <FILENAME>;
close FILENAME;

$temp = @mylist[0] . @mylist[1];
print "$temp";

The filename testtest contains these strings:

1
asdf
asdf

but when i run the code, this is want happens:
Result:
1
asdf
#an empty line

Expected Result:
1asdf

What am I doing wrong?

thanks,
gammaman
 
try this,

open FILENAME, "testtest";
@mylist = <FILENAME>;
close FILENAME;

$temp = (join "",$mylist[0], $mylist[1]);
print "$temp";
 
thanks for the response, but this yields the same result :-(
 
A chomp might be useful as well. After that, there's lots of ways to join the two array elements together... here are a few:
Code:
chomp @mylist;
my $temp = "$mylist[0]$mylist[1]";
my $temp1 = $mylist[0] . $mylist[1];
my $temp2 = join('', @mylist);
 
open FILENAME, "testtest";

while (<FILENAME>)
{
chomp;
push @mylist, $_;
}

$temp = (join "",$mylist[0], $mylist[1]);
print "$temp";

close FILENAME;
 
Only one slight oversight - the text file lines have line-endings you need to remove:-

Code:
open FILENAME, "testtest";
[red][b]chomp ([/b][/red]@mylist = <FILENAME>[b][red])[/red][/b];
close FILENAME;

$temp = @mylist[0] . @mylist[1];
print "$temp";


Kind Regards
Duncan
 
ah, chomp was what i was looking for. thanks guys!
 
the join() function is very slow, just use concatenation or combine strings with double-quotes:

$s = "$r$t";
$s = $r . $t;
 
Code:
$s.=$r;
Just another way of building up a string
--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top