Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi there, it looks like you want to do this:

#!/usr/bin/perl use strict; use warnings; my $file = '1140475.txt';; open my $raw, '<', $file or die "open: $!"; open my $out, '>', "out_$file" or die "close: $!"; while ( my $line = <$raw> ) { chomp( $line ); my @splits = split ( ',', $line ); print $out "First line, first field: $splits[0]\n"; last; } close $out or die "close: $!"; __END__
Output:
$ cat out_1140475.txt First line, first field: 1424621700

But you should use a module if you can. A lot of people like Path::Tiny because it does the file opening, reading, and chomping, for you. In the example below I use split() on the first element of the array of lines returned by Path::Tiny::path() (there's actually only one line since I set the 'count' attribute), and then I assign the first element of the result of split() to $first.

#!/usr/bin/perl use strict; use warnings; use Path::Tiny qw/ path /; my $file = '1140475.txt'; my $first = ( split ',', (path($file)->lines( {chomp => 1, count => 1} + ))[0] )[0]; print "$first\n"; __END__
Output:
$ perl 1140475-2.pl 1424621700 $
Hope this helps!

The way forward always starts with a minimal test.

In reply to Re: How do I select first string of a two dimensional array to compare to other values? by 1nickt
in thread How do I select first string of a two dimensional array to compare to other values? by joi1369

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found