|
Posted by Bill H on July 27, 2008, 12:25 pm
Please log in for more thread options On Jul 27, 9:01=A0am, j...@toerring.de (Jens Thoms Toerring) wrote:
> > 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 =3D `wget -q -O - "http://ws.audioscrobbler.com/1.0/artis=
t/
> > $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 =A0modules. 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 ) {
> =A0 =A0 my @data =3D split /,/, $line;
> =A0 =A0 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.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Regards, Jens
> --
> =A0 \ =A0 Jens Thoms Toerring =A0___ =A0 =A0 =A0j...@toerring.de
> =A0 =A0\__________________________ =A0 =A0 =A0http://toerring.de- Hide qu=
oted text -
>
> - Show quoted text -
I havent used push before, does it perform the same exact function as
using "=3D"? 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] =3D $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
|