Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Turning lines of a file into array?

26 views
Skip to first unread message

Tuxedo

unread,
May 4, 2013, 12:58:37 AM5/4/13
to
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

Charles DeRykus

unread,
May 4, 2013, 2:42:43 AM5/4/13
to
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

Mart van de Wege

unread,
May 4, 2013, 3:39:58 AM5/4/13
to
Tuxedo <tux...@mailinator.com> 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.

J�rgen Exner

unread,
May 4, 2013, 6:24:20 AM5/4/13
to
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


Rainer Weikusat

unread,
May 4, 2013, 9:08:53 AM5/4/13
to
Tuxedo <tux...@mailinator.com> 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

unread,
May 4, 2013, 9:11:06 AM5/4/13
to
Charles DeRykus <der...@gmail.com> 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

unread,
May 4, 2013, 9:13:02 AM5/4/13
to
Rainer Weikusat <rwei...@mssgmbh.com> 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 :-(]

Ben Morrow

unread,
May 4, 2013, 10:05:04 AM5/4/13
to

Quoth Rainer Weikusat <rwei...@mssgmbh.com>:
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

Charles DeRykus

unread,
May 4, 2013, 12:31:53 PM5/4/13
to
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

Tuxedo

unread,
May 9, 2013, 3:02:28 AM5/9/13
to
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

Mart van de Wege

unread,
May 9, 2013, 6:17:02 AM5/9/13
to
Tuxedo <tux...@mailinator.com> 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.

J�rgen Exner

unread,
May 9, 2013, 6:28:17 AM5/9/13
to
Tuxedo <tux...@mailinator.com> wrote:
>system "ls *.JPG > list.txt"; # from remote

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

jue
0 new messages