Textfile to array or hash

J

J.Gluch

Hello,

I want to extract data from a text file I recieve from the internet to
an array or hash. So far I did not found a solution via google and co
- maybe of my bad english and wrong search term.

The file is put into a varaible :
---------------------
$simartistsAS = `wget -q -O - "http://ws.audioscrobbler.com/1.0/artist/
$m/similar.txt"`;
---------------------
and looks like this:
---------------------
100,,Medeski, Martin and Wood
69.54,e0953daa-860f-4dc8-9f1a-b12587cdaf17,Tortoise
49.98,9de8f66e-3cd1-4f11-8328-38200f0612b0,Doves
45.19,b7834ebd-64ae-46c3-a930-2d3a52ee743a,The Walkmen
43.93,ad386705-fb8c-40ec-94d7-e690e079e979,Menomena
36,9dcca4a2-2e05-4f94-9a02-cb97b1beed56,Kenna
30.97,9ef1d76d-5f1d-4398-b4ed-05afb3172f26,Earlimart
30.25,db41efe6-867b-4427-820c-506ea17e5692,The Anniversary
29.69,309c62ba-7a22-4277-9f67-4a162526d18a,Beck
28.11,cbfd7f01-87c3-44bf-a3a2-fe4dbff20ad2,John Scofield
27.88,bd837f10-ed18-47e9-9636-ca44edceebfd,Elefant
---------------------
I want to have either an array or hash that contains all artist names
(the string after the second comma, that have rating higher than 50
(the first number until first coma).
For this task I do not want to use any aditional modules. How do
process the data?

Juergen Gluch
 
J

Jens Thoms Toerring

J.Gluch said:
I want to extract data from a text file I recieve from the internet to
an array or hash. So far I did not found a solution via google and co
- maybe of my bad english and wrong search term.
The file is put into a varaible :
---------------------
$simartistsAS = `wget -q -O - "http://ws.audioscrobbler.com/1.0/artist/
$m/similar.txt"`;
---------------------
and looks like this:
---------------------
100,,Medeski, Martin and Wood
69.54,e0953daa-860f-4dc8-9f1a-b12587cdaf17,Tortoise
49.98,9de8f66e-3cd1-4f11-8328-38200f0612b0,Doves
45.19,b7834ebd-64ae-46c3-a930-2d3a52ee743a,The Walkmen
43.93,ad386705-fb8c-40ec-94d7-e690e079e979,Menomena
36,9dcca4a2-2e05-4f94-9a02-cb97b1beed56,Kenna
30.97,9ef1d76d-5f1d-4398-b4ed-05afb3172f26,Earlimart
30.25,db41efe6-867b-4427-820c-506ea17e5692,The Anniversary
29.69,309c62ba-7a22-4277-9f67-4a162526d18a,Beck
28.11,cbfd7f01-87c3-44bf-a3a2-fe4dbff20ad2,John Scofield
27.88,bd837f10-ed18-47e9-9636-ca44edceebfd,Elefant
---------------------
I want to have either an array or hash that contains all artist names
(the string after the second comma, that have rating higher than 50
(the first number until first coma).
For this task I do not want to use any aditional modules. How do
process the data?

As usual, there are lots of ways to skin the cat. This one is
hopefully easy to understand:

my @artists;
for my $line ( split /\n/, $similarArtistsAS ) {
my @data = split /,/, $line;
push @artists, $data[ 2 ] if $data[ 0 ] > 50;
}

The loop runs over all lines (splitting the content of '$similarArtistsAS'
up on the end-of-line character). Each line itself gets split up on the
commas. You push the third element of the result (the stuff after the
second comma) on the array if the first element of the result is larger
than 50.
Regards, Jens
 
J

Jürgen Exner

J.Gluch said:
I want to extract data from a text file I recieve from the internet to
an array or hash. So far I did not found a solution via google and co [...]
28.11,cbfd7f01-87c3-44bf-a3a2-fe4dbff20ad2,John Scofield
27.88,bd837f10-ed18-47e9-9636-ca44edceebfd,Elefant

while() there are still lines to process
split() each line at ','
if the first element is > than 50
then push() the third element onto the result

jue
 
B

Bill H

J.Gluch said:
I want to extract data from a text file I recieve from the internet to
an array or hash. So far I did not found a solution via google and co
- maybe of my bad english and wrong search term.
The file is put into a varaible :
---------------------
$simartistsAS = `wget -q -O - "http://ws.audioscrobbler.com/1.0/artist/
$m/similar.txt"`;
---------------------
and looks like this:
---------------------
100,,Medeski, Martin and Wood
69.54,e0953daa-860f-4dc8-9f1a-b12587cdaf17,Tortoise
49.98,9de8f66e-3cd1-4f11-8328-38200f0612b0,Doves
45.19,b7834ebd-64ae-46c3-a930-2d3a52ee743a,The Walkmen
43.93,ad386705-fb8c-40ec-94d7-e690e079e979,Menomena
36,9dcca4a2-2e05-4f94-9a02-cb97b1beed56,Kenna
30.97,9ef1d76d-5f1d-4398-b4ed-05afb3172f26,Earlimart
30.25,db41efe6-867b-4427-820c-506ea17e5692,The Anniversary
29.69,309c62ba-7a22-4277-9f67-4a162526d18a,Beck
28.11,cbfd7f01-87c3-44bf-a3a2-fe4dbff20ad2,John Scofield
27.88,bd837f10-ed18-47e9-9636-ca44edceebfd,Elefant
---------------------
I want to have either an array or hash that contains all artist names
(the string after the second comma, that have rating higher than 50
(the first number until first coma).
For this task I do not want to use any aditional  modules. How do
process the data?

As usual, there are lots of ways to skin the cat. This one is
hopefully easy to understand:

my @artists;
for my $line ( split /\n/, $similarArtistsAS ) {
    my @data = split /,/, $line;
    push @artists, $data[ 2 ] if $data[ 0 ] > 50;

}

The loop runs over all lines (splitting the content of '$similarArtistsAS'
up on the end-of-line character). Each line itself gets split up on the
commas. You push the third element of the result (the stuff after the
second comma) on the array if the first element of the result is larger
than 50.
                              Regards, Jens
--
  \   Jens Thoms Toerring  ___      (e-mail address removed)
   \__________________________      http://toerring.de- Hide quoted text -

- Show quoted text -

I havent used push before, does it perform the same exact function as
using "="? So in you sexample:

push @artists, $data[ 2 ] if $data[ 0 ] > 50;

Would this be the exact equivalent of doing:

if ($data[0] > 50){$artists[@artists] = $data[2];}

Or does more happen with push? Just for the record the last time I
ever used a "push" command was in Z80 ML programming on a Timex
Sinclair 1000 (bout 3 years ago).

Bill H
 
J

Jens Thoms Toerring

Bill H said:
I havent used push before, does it perform the same exact function as
using "="? So in you sexample:
push @artists, $data[ 2 ] if $data[ 0 ] > 50;
Would this be the exact equivalent of doing:
if ($data[0] > 50){$artists[@artists] = $data[2];}

Yes, here it does. It simply appends '$data[2]' to the end of
the '@artists' array. And it does it more efficiently (at
least that's what the documentation claims).
Or does more happen with push?

Well, you can also 'push' a whole list, thus appending a number
of new elements to the array. So you can e.g. do

my @a = ( 1, 2, 3 );
my @b = ( 4, 5, 6 );
push @a, @b;

to concatenate @a and @b. And it returns the new number of
elements of the array.
Just for the record the last time I
ever used a "push" command was in Z80 ML programming on a Timex
Sinclair 1000 (bout 3 years ago).

It's a bit similar as far as I remember (but it's nearly 20
years since I last used a Z80). There the PUSH command moved
the content of a register onto the stack, automatically de-
crementing the stack pointer by two (and POP moved data back
from the stack into a register and incremented SP twice).
Perl's push() and pop() functions allow you to use each array
as a kind of stack.
Regards, Jens
 
J

Jürgen Exner

Bill H said:
I havent used push before, does it perform the same exact function as
using "="?

Why don't you simply check the documentation that came with your Perl
installation?
perldoc -f push

To answer your direct question: No.
"=" can be used to assign to any variable, push() is limited to arrays.
So in you sexample:

push @artists, $data[ 2 ] if $data[ 0 ] > 50;

Would this be the exact equivalent of doing:

if ($data[0] > 50){$artists[@artists] = $data[2];}

Yes, except for the return value of this construct.
Or does more happen with push? Just for the record the last time I

Yes, ist also sets the return value to the new number of elements in the
array. This is explained in the documentation.

jue
 
B

Bill H

Bill H said:
I havent used push before, does it perform the same exact function as
using "="?

Why don't you simply check the documentation that came with your Perl
installation?
        perldoc -f push

To answer your direct question: No.
"=" can be used to assign to any variable, push() is limited to arrays.
So in you sexample:
push @artists, $data[ 2 ] if $data[ 0 ] > 50;
Would this be the exact equivalent of doing:
if ($data[0] > 50){$artists[@artists] = $data[2];}

Yes, except for the return value of this construct.
Or does more happen with push? Just for the record the last time I

Yes, ist also sets the return value to the new number of elements in the
array. This is explained in the documentation.

jue

I'm going to do that Jurgen. Every now and then I see a command used
on here that I haven't used before and I add it to my daily usage. The
push seems like a nifty one.

Bill H
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top