Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

split line from one point

by Anonymous Monk
on Oct 19, 2011 at 12:04 UTC ( #932375=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, i have a file and i want to split each line to before "T" and after that. Unfortunately , my code doesn't work.please help me!

2010-12-01T20:15:20.000+01:00 2010-12-01T21:10:18.000+01:00 2010-12-01T21:15:40.000+01:00 2010-12-01T23:28:04.000+01:00 2010-12-01T20:16:33.000+01:00 to for example 2010-12-01,20:15:20.000+01:00
#!usr/bin/perl -w open(IN,'<transaction'); open(RE,'>>res'); while(<IN>) { $a=split(/T/); print RE $a;} close IN; close RE;

Comment on split line from one point
Select or Download Code
Re: split line from one point
by moritz (Cardinal) on Oct 19, 2011 at 12:17 UTC

    Please be more specific than "doesn't work". What happens? Do blue flames come out of your computer?

    Anyway, the problem is likely that you call split in scalar context. Depending on your Perl version, that either puts the number of chunks into $a (newer perls), or does something very weird (in older perl, it writes the chunks to @_).

    The solution is to call split in list context:

    my ($a) = split /T/; print RE $a;

      thanks, i have this: read line <> on closed file handle IN at split.pl line 4.

        That means that the first open() call failed. Either use autodie; in the beginning, or write your calls as
        open my $IN, '<', 'transaction' or die "Cannot open 'transaction' for +reading: $!"; ... while (<$IN>) { ... }

        (And adapt the code for the second file accordingly).

Re: split line from one point
by toolic (Chancellor) on Oct 19, 2011 at 12:27 UTC
    What moritz said, and if you want to keep what is before and after the T, split into an array:
    use warnings; use strict; while (<DATA>) { my @items = split /T/; print "@items"; } __DATA__ 2010-12-01T20:15:20.000+01:00 2010-12-01T21:10:18.000+01:00 2010-12-01T21:15:40.000+01:00 2010-12-01T23:28:04.000+01:00 2010-12-01T20:16:33.000+01:00
    prints...
    2010-12-01 20:15:20.000+01:00 2010-12-01 21:10:18.000+01:00 2010-12-01 21:15:40.000+01:00 2010-12-01 23:28:04.000+01:00 2010-12-01 20:16:33.000+01:00

      it is strange because again the problem was repeated. read line error! i didn't have before this problem. i think it is because of changing the Perl version

        But what "read line error"? What is the string value of  $! (see perlvar) associated with the error?

Re: split line from one point
by Anonymous Monk on Oct 19, 2011 at 12:31 UTC

    See Tutorials: Context in Perl: Context tutorial

    split, in scalar context, returns the number of fields found, so you need to use list context

    See also autodie and Modern Perl: the free book

    #!/usr/bin/perl -- #~ 2011-10-19-05:27:59PDT by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use autodie; # dies if open/close... fail Main( @ARGV ); exit( 0 ); sub Main { if ( @_ == 2 ) { NotDemoMeaningfulName(@_); } else { Demo(); print '#' x 33 ,"\n", Usage(); } } ## end sub Main sub NotDemoMeaningfulName { my( $transaction, $res ) = @_; open my($in), '<', $transaction; open my($re), '>', $res; while( <$in> ){ my( $left, $right ) = split /T/, $_, 2; print $re $left; } close $in; close $re; } ## end sub NotDemoMeaningfulName sub Usage { <<"__USAGE__"; $0 $0 dataFile newDataFile perl ${\__FILE__} perl ${\__FILE__} dataFile newDataFile __USAGE__ } ## end sub Usage sub Demo { my ( $Input, $WantedOutput ) = DemoData(); NotDemoMeaningfulName( \$Input, \my $Output ); require Test::More; Test::More::is( $Output, $WantedOutput, ' NotDemoMeaningfulName Works Aas Designed' ); Test::More::done_testing(); } ## end sub Demo sub DemoData { my $One = <<'__One__'; 2010-12-01T21:15:40.000+01:00 2010-12-01T23:28:04.000+01:00 __One__ my $Two = <<'__Two__'; 2010-12-01 2010-12-01 __Two__ return $One, $Two; } ## end sub DemoData __END__
Re: split line from one point
by raybies (Chaplain) on Oct 19, 2011 at 14:47 UTC

    While all the above posts are correct, you're not understanding what "split" does...

    what you want is the substitution operator:

    s/T/,/;

    (Essentially you're trying to substitute the 'T' for the ',') then just print, you don't even need the $a variable.

    cheers!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932375]
Approved by moritz
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-10-31 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls