![]() |
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.
Code:
$s="1232423#asdf#124324#54534#dcfg#wert#rrftt#4567"; |
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: Code:
#! /usr/bin/perl |
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. |
| All times are GMT -4. The time now is 08:14 PM. |
Powered by: vBulletin,
Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums