The UNIX and Linux Forums

The UNIX and Linux Forums (http://www.unix.com/index.php)
-   UNIX for Advanced & Expert Users (http://www.unix.com/unix-for-advanced-and-expert-users/)
-   -   Split Command in Perl (http://www.unix.com/unix-for-advanced-and-expert-users/34579-split-command-perl.html)

rochitsharma 01-19-2007 07:02 AM

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

ghostdog74 01-19-2007 07:13 AM

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";
@fields=split('#',$s);
if( $fields[3] eq "54534")
{
    print "found"
}


nathan 01-19-2007 07:42 AM

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

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

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


rochitsharma 01-19-2007 07:43 AM

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

cbkihong 01-19-2007 09:09 AM

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.

rochitsharma 01-19-2007 09:24 AM

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

vshar 02-09-2007 01:01 PM

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 02:05 AM.

Linux and Unix Supported by: vBulletin
Search Engine Optimisation provided by DragonByte SEO v1.1.4 (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums