Turning lines of a file into array?

T

Tuxedo

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
 
C

Charles DeRykus

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.
 
M

Mart van de Wege

Tuxedo said:
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
 
J

Jürgen Exner

Tuxedo said:
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
 
R

Rainer Weikusat

Tuxedo said:
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;
 
R

Rainer Weikusat

[...]
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.
 
R

Rainer Weikusat

[...]
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 :-(]
 
C

Charles DeRykus

[...]
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.
 
T

Tuxedo

Ben said:
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
 
M

Mart van de Wege

Tuxedo said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top