Split Command in Perl

Hi,

I have to split a line of the form

1232423#asdf#124324#54534#dcfg#wert#rrftt#4567

into an array in perl. I am using

@fields;
@fields=split('#',$line);
if($fields[3] eq "1")

But this is not working. By using the syntax, the statements in "if" are never executed. Please help. Thanks in advance.

Regards
Rochit

you did not provide the whole code. you can stop at the if statement just like that.

$s="1232423#asdf#124324#54534#dcfg#wert#rrftt#4567";
@fields=split('#',$s);
if( $fields[3] eq "54534")
{
    print "found"
}

ghostdog is correct, your statement below would never be true. For one, arrays start numbering at 0 , not 1, so fields[3] would be 54534.

Also, fields[3] wouldn't equal "1" or "5" , it would just start with that character. You should use a pattern match, if that's what you're trying to do. Something like this should work:

#! /usr/bin/perl

$line = "1232423#asdf#124324#54534#dcfg#wert#rrftt#4567";

@fields=split('#',$line);
if( $fields[2] =~ /^1/ )
{
   print "true\n";
}

hi

My actual code looks like below one..

@fields;
$sum=0;

$count=0;
$path_ascii="$ARGV[0]";
$outpath="/home/rout";
open(FW,">$outpath/test100.out");
opendir(DIR,"$path_ascii");
while($file = readdir(DIR))
{
open (RR,"<$path_ascii/$file");
print "\n$file";
while ( $line = <RR> )
{
@fields=split('#',$line);
if($fields[3] eq "1")
{
print "4";
}

When I execute it 4 is not Printed. The problem I have found is that its not entering into "if" statement. Please help.

Regards
Rochit

Did you actually try print()ing the content of @fields on each iteration while debugging? Your sample format does not suggest any field that has the content of exactly "1" so if that does not match "1" it is not really unusual.

there are more input lines like

12213#adsda#1232#1#eqrsd#weq#13442#qwe
21321#asrdsad#234#1#wer#wqr#23421#ewrt

so the field 1 does exists...

I don't see anything wrong in the if statement, I tested it and it works fine.

$line= "12213#adsda#1232#1#eqrsd#weq#13442#qwe";
print "[",$line,"]\n";
@fields;
@fields=split('#',$line);
if($fields[3] eq "1")
{
print "found 1\n";
}
$line= "12213#adsda#1232#2351#eqrsd#weq#13442#qwe";
print "[",$line,"]\n";
@fields;
@fields=split('#',$line);
if($fields[3] eq "1")
{
print "found 1\n";
}
else
{
print "1 not found\n";
}

Output is:

[12213#adsda#1232#1#eqrsd#weq#13442#qwe]
found 1
[12213#adsda#1232#2351#eqrsd#weq#13442#qwe]
1 not found

Try to print the line before if statement and check what value you get in $line variable.

try printing the $line value after reading from RR, see if anything is being read from the file. If so, try also printing the array after the split with:
print "line is $line\nFields are:\n". join ("\n", @fields) . "\n";

I am sorry i am replying your post, with new problem.
I am trying to get vlue of X positive and Negative. i am using array. It did not do the calculation part but i got out put
Cofficients: 2 5 1

Can not do the negetive calculations

Illegal division by zero at pgm4.pl line 31, <> line 1.

so i think some thing wrong in if statement i could not figuring out where is mistake. I will really apriciate it if someone show me the way.

print "Cofficients: ";

$input =<>;
@input = split(' ', $input);
($vol, $vol1, $ vol2) = @input;

print "\n";

$test0 = ($vol1**2-4*($vol*$vol2));
if (test0 <1)
{
print "Can not do the negetive calculations\n\n";
}
$test1 = (2*$vol);
if ($test1 <= 0)
{
print "Can not divide by zero\n\n";
}

   $x = \(-$vol \+ sqrt\($test0\)\)/\(test1\);

$negative = (-$vol - sqrt($test0))/(test1);

if ($x = $negative)
{
print "$x";
exit;
}
else
{
print "$x\n $negative";
exit;
}
Thank you in Advance:confused:

Hi,
In the division statements the test1 variable is missing the '$' sign.
Moshe