Turning lines of a file into array?

Discussion in 'Perl Misc' started by Tuxedo, May 4, 2013.

  1. Tuxedo

    Tuxedo Guest

    Sorry for the basic question but how can I best turn each line of a file
    into an array?

    I have a list filenames (just output of 'ls') in a separate text file, each
    on a new line:
    DSC07557.JPG
    DSC07532.JPG
    DSC07563.JPG
    etc.

    The resulting perl array needed is the above plus a fixed URL string
    before, in order to fetch all in a later procedure, such as wget or
    likewise.

    Thanks for any tips.
    Tuxedo
    Tuxedo, May 4, 2013
    #1
    1. Advertising

  2. On 5/3/2013 9:58 PM, Tuxedo wrote:
    > I have a list filenames (just output of 'ls') in a separate text file, each
    > on a new line:
    > DSC07557.JPG
    > DSC07532.JPG
    > DSC07563.JPG
    > etc.
    >
    > The resulting perl array needed is the above plus a fixed URL string
    > before, in order to fetch all in a later procedure, such as wget or
    > likewise.


    For a Unix-y quick way:

    my @array = qx{cat /path/to/myfile};
    die "error: .... : $?" if $?;
    unshift @array, "some fixed string...";


    see: perlfaq -q "line in a file" or modules Tie::File or File::Slurp for
    other ways.

    --
    Charles DeRykus
    Charles DeRykus, May 4, 2013
    #2
    1. Advertising

  3. Tuxedo <> writes:

    > Sorry for the basic question but how can I best turn each line of a file
    > into an array?
    >
    > I have a list filenames (just output of 'ls') in a separate text file, each
    > on a new line:
    > DSC07557.JPG
    > DSC07532.JPG
    > DSC07563.JPG
    > etc.
    >

    The Line Input operator <> is meant for this if you use it in list
    context.

    Let say you have those three lines in a text file, then the code would
    be like this:

    use strict;
    use warnings;
    use Data::Dumper;

    open(my $fh, "<", "files.txt")
    or die "can't open files.txt: $!";
    my @lines = <$fh>;

    You now have an array with the filenames in @lines.

    > The resulting perl array needed is the above plus a fixed URL string
    > before, in order to fetch all in a later procedure, such as wget or
    > likewise.


    It depends on where you want the URL to be, just use push or unshift.

    Mart

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
    Mart van de Wege, May 4, 2013
    #3
  4. Tuxedo <> wrote:
    >Sorry for the basic question but how can I best turn each line of a file
    >into an array?
    >
    >I have a list filenames (just output of 'ls') in a separate text file, each
    >on a new line:
    >DSC07557.JPG
    >DSC07532.JPG
    >DSC07563.JPG
    >etc.
    >
    >The resulting perl array needed is the above plus a fixed URL string
    >before, in order to fetch all in a later procedure, such as wget or
    >likewise.


    So each array should contain this URL as the first element and the line
    from the file as second element?

    while (<$F>){
    my @thisarray = ($SomeURL, $_);
    }

    jue
    Jürgen Exner, May 4, 2013
    #4
  5. Tuxedo <> writes:
    > Sorry for the basic question but how can I best turn each line of a file
    > into an array?
    >
    > I have a list filenames (just output of 'ls') in a separate text file, each
    > on a new line:
    > DSC07557.JPG
    > DSC07532.JPG
    > DSC07563.JPG
    > etc.
    >
    > The resulting perl array needed is the above plus a fixed URL string
    > before,


    -----------------
    my ($fh, @data);

    open($fh, '<', '/tmp/data') // die($!);
    @data = map { chomp; "string $_"; } <$fh>;

    print(join(' ', $_), "\n") for @data;
    Rainer Weikusat, May 4, 2013
    #5
  6. Charles DeRykus <> writes:

    [...]

    > my @array = qx{cat /path/to/myfile};
    > die "error: .... : $?" if $?;
    > unshift @array, "some fixed string...";


    Evaluating a file handle in angular brackets in list context results
    in list composed of all lines of text in the file.
    Rainer Weikusat, May 4, 2013
    #6
  7. Rainer Weikusat <> writes:

    [...]

    > my ($fh, @data);
    >
    > open($fh, '<', '/tmp/data') // die($!);
    > @data = map { chomp; "string $_"; } <$fh>;
    >
    > print(join(' ', $_), "\n") for @data;


    The 'join' was left over from an earlier version which used

    ['string', $_]

    as 'map operation' [it was join(' ', @$_) back then :-(]
    Rainer Weikusat, May 4, 2013
    #7
  8. On 5/4/2013 6:11 AM, Rainer Weikusat wrote:
    > Charles DeRykus <> writes:
    >
    > [...]
    >
    >> my @array = qx{cat /path/to/myfile};
    >> die "error: .... : $?" if $?;
    >> unshift @array, "some fixed string...";

    >
    > Evaluating a file handle in angular brackets in list context results
    > in list composed of all lines of text in the file.
    >


    Yes but there was no mention of a filehandle - only having
    a file. Opening the file natively in perl is fine but after
    all, Perl's a glue language. especially, if you're just
    doing something simple and want to save a few keystrokes.


    --
    Charles DeRykus
    Charles DeRykus, May 4, 2013
    #8
  9. Tuxedo

    Tuxedo Guest

    Ben Morrow wrote:

    >
    > Quoth Rainer Weikusat <>:
    > > Tuxedo <> writes:
    > > > Sorry for the basic question but how can I best turn each line of a
    > > > file into an array?
    > > >
    > > > I have a list filenames (just output of 'ls') in a separate text file,
    > > > each on a new line:
    > > > DSC07557.JPG
    > > > DSC07532.JPG
    > > > DSC07563.JPG
    > > > etc.
    > > >
    > > > The resulting perl array needed is the above plus a fixed URL string
    > > > before,

    > >
    > > -----------------
    > > my ($fh, @data);
    > >
    > > open($fh, '<', '/tmp/data') // die($!);
    > > @data = map { chomp; "string $_"; } <$fh>;

    >
    > I find rather interesting the number of different ways people have
    > interpreted 'plus a fixed URL string before' :). Thinking about it I
    > suspect this is the interpretation the OP wanted, though I would have
    > guessed 'unshift' (or rather, my @ary = $url, <$FH>)...
    >
    > Just goes to show, it pays to be as clear as possible when asking for
    > help.
    >
    > Of course, given 'just output of 'ls'', it's possible that what the OP
    > is actually looking for is list-context readdir, but without more
    > information it's impossible to tell.
    >
    > Ben
    >


    Many thanks for all the replies in showing the numerous ways of doing more
    or less the same thing. In the end I just used somothing as follows, as
    taken from one of the replies:

    system "ls *.JPG > list.txt"; # from remote

    open (my $fh, "<", "list.txt") or die "can't open file: $!";
    my @lines = <$fh>;

    my $array_entry;

    system "wget --delete-after --secure-protocol=auto --http-user=***
    --http-password=*** \"http://***/img.php?f=$array_entry&x=200\"";

    }

    Tuxedo
    Tuxedo, May 9, 2013
    #9
  10. Tuxedo <> writes:

    > Many thanks for all the replies in showing the numerous ways of doing more
    > or less the same thing. In the end I just used somothing as follows, as
    > taken from one of the replies:
    >
    > system "ls *.JPG > list.txt"; # from remote
    >
    > open (my $fh, "<", "list.txt") or die "can't open file: $!";
    > my @lines = <$fh>;
    >
    > my $array_entry;
    >
    > system "wget --delete-after --secure-protocol=auto --http-user=***
    > --http-password=*** \"http://***/img.php?f=$array_entry&x=200\"";
    >
    > }
    >

    I think some code disappeared there. I assume you are iterating over the
    array?

    Your code then should look something like:

    for my $array_entry (@lines) {
    system etc...
    }

    That, however, makes reading the file redundant, as there is an idiom
    for that:

    while (my $array_entry = <$fh>) {
    system etc...
    }

    This is standard idiom to read a file line-by-line, the <$fh> line input
    operator in scalar context will read the next line until EOF, when it
    will return false and terminate the while loop.

    But if you're going this route, you don't have to first redirect to a
    file. Your code above assumes you're running the script in the directory
    the files are in, so instead of using system to use ls to create a file
    with all *.JPG files and then iterating over every line in the file, you
    can use the glob operator instead (which, confusingly, looks like the
    line-input operator):

    my @jpgs = <*.JPG>

    for my $array_entry (@jpgs) {
    system etc...
    }

    And even more perly would be to replace the system call with some code
    using LWP::UserAgent or WWW::Mechanize.

    Mart

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
    Mart van de Wege, May 9, 2013
    #10
  11. Tuxedo <> wrote:
    >system "ls *.JPG > list.txt"; # from remote


    No need to shell out an external process, please see
    perldoc -f readdir

    jue
    Jürgen Exner, May 9, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Row
    Replies:
    0
    Views:
    480
  2. David Garamond
    Replies:
    6
    Views:
    122
    Robert Klemme
    Nov 26, 2003
  3. David Garamond

    turning a hash into an array

    David Garamond, Jan 23, 2004, in forum: Ruby
    Replies:
    2
    Views:
    73
    Robert Klemme
    Jan 26, 2004
  4. Replies:
    1
    Views:
    113
  5. Replies:
    36
    Views:
    312
    Gunnar Hjalmarsson
    Mar 13, 2006
Loading...

Share This Page