Split Command in Perl


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Split Command in Perl
# 1  
Old 01-19-2007
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
# 2  
Old 01-19-2007
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"
}

# 3  
Old 01-19-2007
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";
}

# 4  
Old 01-19-2007
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
# 5  
Old 01-19-2007
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.
# 6  
Old 01-19-2007
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...
# 7  
Old 02-09-2007
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.
vshar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl split and array

Hello, I have the following code: while ($line = <fd_in>) { 126 $line = " " . $line ; 127 print "our_line:$line\n"; 128 @list = split (/\s+/, $line) ; 129 print "after_split:@list\n"; 130 print "$list\t$list\t$list\t$list\t$list\t$list$list\t\n"; 131 $len =... (2 Replies)
Discussion started by: Zam_1234
2 Replies

2. Shell Programming and Scripting

Split the string in perl

Hi All, How to split the string KAR_Celltick_Ban_GSMGW3 and want to pickup the third filed. Sometime the string may be "KAR_Celltick_Ban" like this Thanks in advance (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

3. Shell Programming and Scripting

using awk in perl with split command

Hi, I have an array with following data. First field shows the owner and second is unique name. Now i have to pic the latest value with respect to the date in case of duplicate. like "def" is from two owners "rahul/vineet", now i want the latest from the two and the owner name also for all the... (9 Replies)
Discussion started by: vineet.dhingra
9 Replies

4. Shell Programming and Scripting

Perl split and join

Hi, I have tried the split and join functions but stuck with unexpected results. Any help appreciated. I pass multiple values at command line like perl test.pl -type java,xml. This works good for me but i am not sure how to print it in the required format. Here is the code i tried:... (4 Replies)
Discussion started by: nmattam
4 Replies

5. Programming

Perl Split Issue

Hello All - I am having trouble with the split command in perl. Here is what I am trying to do. 75|2455345|2455349|00:00:00|00:00:00|Once|0|Frank Vacation | | 5 I want to only print out the 8th column. So that would be "Frank Vacation" and "Power Down" I need to run this on a file that... (16 Replies)
Discussion started by: tarken
16 Replies

6. Shell Programming and Scripting

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

7. Homework & Coursework Questions

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

8. Shell Programming and Scripting

Perl split question

hi, I have a seemingly really stupid question, but here goes! What do you enter into split delimiter to seperate something like this "December 12, 1995" and get December 12 1995 ? thanks (5 Replies)
Discussion started by: ade214
5 Replies

9. Shell Programming and Scripting

Use split function in perl

Hello, if i have file like this: 010000890306932455804 05306977653873 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC30693599000 30971360000 ZZZZZZZZZZZZZZZZZZZZ202011302942311 010000890306946317387 05306977313623 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC306942190000 30971360000... (5 Replies)
Discussion started by: chriss_58
5 Replies

10. Shell Programming and Scripting

Perl Split Command usage

Hi, I am trying to use the split commad to seperate string reading from file. but it dosent give me a correct result. can some body tell me what is the wrong in following scritp. #!/usr/bin/perl -w #use CGI qw(:standard); ... (2 Replies)
Discussion started by: maheshsri
2 Replies
Login or Register to Ask a Question