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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatenation problms and Perl 5.8

Status
Not open for further replies.

Graziella

Programmer
Jul 8, 2004
38
AT
I am having problems concatenating variables and
printing the concatenated string.
I really hope you can help me because I have never seen this behavior in
Perl
and my only explanation is that there is something in Perl 5.8 that I do
not
know how to use.

Input file (ID, pattern pairs) looks like this
1412 Owens
1412 (Roy_)?Romer
1418 1951
1419 1959
1419 1958
and as you can see some IDs are the same: I am putting it into a hash, with
the ID being the key. When the same ID has multiple values, then I want to
concatenate its values and store them at the same ID.
Piece of code in question is an easy hash formation:
open (P_FILE, "<$p_file") || die "Can't open $p_file\n";


while(<P_FILE>){
my($line) = $_;
chomp($line);

($id, $pattern) = ($line =~ /^(\d+)\s+(.*)/);

if (!exists ($ID2patterns{$id}) ) {
$ID2patterns{$id} = $pattern;
} else {
my($old) = $ID2patterns{$id};
my($new) = $pattern;
my($joined_patterns) = $old."<=>".$new;
$ID2patterns{$id} = $joined_patterns;
}
print "HELLO $ID2patterns{$id}\n";
}
close(P_FILE);


When I go to print things, this is what I get:

HELLO Owens
<=>(Roy_)?Romer
HELLO 1951
HELLO 1959
<=>1958959

The output looks bizarre to me.

Is this an encoding problem ?!

Thank you,

Grazia
 
this is an operator:

<=>

try single quoting it and see if that helps:

'<=>'

just a guess, I didn't try your code.



 
I tried your code and got this output:
Code:
HELLO Owens
HELLO Owens<=>(Roy_)?Romer
HELLO 1951
HELLO 1959
HELLO 1959<=>1958
which is what I would expect. I'm using Perl 5.8 on WinXP. Given your code and data as posted I don't see anything that would account for the bizarre output you claim you're getting. I think there must be something about your code or your data or something that you're not showing us or telling us. Is there anything else we should know?
 
I think that the input file had some encoding issues.
There must have been characters I could not see with my browser that caused problems with the regex.
I copied and pasted the input files into a .txt file and the problem has disappeared. I changed nothing in my code.

Thanks for your replies.

Grazia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top