FAQ
J. Patrick Lanigan writes ..

I haven't quite sorted out the more complex data structure in perl yet.
Anyhow, I need to take the following hash of arrays...

you have some very confusing code there .. so let's do it one bit at a time

my %tracks = ();
create a hash called tracks with zero elements

push @{$tracks{$filename}},
$_, # tracks.filename
$File::Find::dir . '/', # tracks.filepath
$artist, # artist.artist_name
$album, # album.title
$tracknum, # tracks.track_num
$title, # tracks.title
$genre; # tracks.title
create a new element of the tracks hash using the value of the variable
$filename as the key

treat that new element as an array reference and push a bunch of values onto
it

assume for a secont that $filename has the value 'file1' .. at this point in
the code you have something like the following structure

%tracks = ( file1 => [ $_, 'some/path/', 'artist_name', 'etc...' ] );

I didn't continue it .. but as well as the artist name you've got the album
title, the track number, title, and genre in there .. you get the drift

but you've got only one member of the %tracks hash .. it's key is the value
of $filename and it's value is an array reference as above

for my $row ( keys %tracks ){
so now we grab the keys of %tracks - of which there is only one - 'file1' in
our example .. then we stick that into $row
(my $qualified_filename, my $filename, my $filepath,
$artist, $album, $tracknum, $title, $genre) =
"$row @{ $tracks{$row} }";
now we create a string containing the key ('file1') then a space and then
the values of the array referred to by $tracks{file1}

it's a single value - so it goes into the first element of the lvalue list -
ie. $qualified_filename

the following code would have done EXACTLY the same as what you've done
above

my %tracks;

$tracks{$filename} = [ $_,
$File::Find::dir . '/',
$artist,
$album,
$tracknum,
$title,
$genre,
];

my $qualified_name = "$filename @{ $tracks{$filename} }";

so .. as you can tell - you'll have to explain a bit more about what you're
trying to do .. because it's really not clear

--
jason king

A Canadian law states that citizens may not publicly remove bandages.
- http://dumblaws.com/

Search Discussions

  • J. Patrick Lanigan at May 1, 2001 at 3:08 pm
    Overall what I am trying to accomplish is to loop through a bunch of mp3
    files and extract the ID3 Tag Info into a hash (%tracks) for which each key
    references the filename, filepath, artist, album, tracknum, title, genre for
    one file. Once I have %tracks populated I need to iterate through it and
    INSERT the info into a PostgreSQL DB. I am completely open to any other
    suggestion for a way to collect a dataset with a unique identifier. I've got
    all the other code working, this is the last piece to my puzzel.

    Thanks,
    Patrick

    Jason King wrote:

    J. Patrick Lanigan writes ..

    I haven't quite sorted out the more complex data structure in perl yet.
    Anyhow, I need to take the following hash of arrays...

    you have some very confusing code there .. so let's do it one bit
    at a time

    my %tracks = ();
    create a hash called tracks with zero elements

    push @{$tracks{$filename}},
    $_, # tracks.filename
    $File::Find::dir . '/', # tracks.filepath
    $artist, # artist.artist_name
    $album, # album.title
    $tracknum, # tracks.track_num
    $title, # tracks.title
    $genre; # tracks.title
    create a new element of the tracks hash using the value of the variable
    $filename as the key

    treat that new element as an array reference and push a bunch of
    values onto
    it

    assume for a secont that $filename has the value 'file1' .. at
    this point in
    the code you have something like the following structure

    %tracks = ( file1 => [ $_, 'some/path/', 'artist_name', 'etc...' ] );

    I didn't continue it .. but as well as the artist name you've got
    the album
    title, the track number, title, and genre in there .. you get the drift

    but you've got only one member of the %tracks hash .. it's key is
    the value
    of $filename and it's value is an array reference as above

    for my $row ( keys %tracks ){
    so now we grab the keys of %tracks - of which there is only one -
    'file1' in
    our example .. then we stick that into $row
    (my $qualified_filename, my $filename, my $filepath,
    $artist, $album, $tracknum, $title, $genre) =
    "$row @{ $tracks{$row} }";
    now we create a string containing the key ('file1') then a space and then
    the values of the array referred to by $tracks{file1}

    it's a single value - so it goes into the first element of the
    lvalue list -
    ie. $qualified_filename

    the following code would have done EXACTLY the same as what you've done
    above

    my %tracks;

    $tracks{$filename} = [ $_,
    $File::Find::dir . '/',
    $artist,
    $album,
    $tracknum,
    $title,
    $genre,
    ];

    my $qualified_name = "$filename @{ $tracks{$filename} }";

    so .. as you can tell - you'll have to explain a bit more about
    what you're
    trying to do .. because it's really not clear

    --
    jason king

    A Canadian law states that citizens may not publicly remove bandages.
    - http://dumblaws.com/
  • Gary Stainburn at May 1, 2001 at 3:22 pm
    Surely, a hash of hashes would be better then.

    my %tracks=(); # create empty hash

    Then to populate the data you do something like:

    $tracks{$fname}->{artist}=$artist;
    $tracls{$fname}->{title}=$title;

    Gary
    On Tuesday 01 May 2001 4:08 pm, J. Patrick Lanigan wrote:
    Overall what I am trying to accomplish is to loop through a bunch of
    mp3 files and extract the ID3 Tag Info into a hash (%tracks) for
    which each key references the filename, filepath, artist, album,
    tracknum, title, genre for one file. Once I have %tracks populated I
    need to iterate through it and INSERT the info into a PostgreSQL DB.
    I am completely open to any other suggestion for a way to collect a
    dataset with a unique identifier. I've got all the other code
    working, this is the last piece to my puzzel.

    Thanks,
    Patrick
    Jason King wrote:

    J. Patrick Lanigan writes ..
    I haven't quite sorted out the more complex data structure in perl
    yet. Anyhow, I need to take the following hash of arrays...
    you have some very confusing code there .. so let's do it one bit
    at a time
    my %tracks = ();
    create a hash called tracks with zero elements
    push @{$tracks{$filename}},
    $_, # tracks.filename

    $File::Find::dir . '/', # tracks.filepath
    $artist, # artist.artist_name
    $album, # album.title
    $tracknum, # tracks.track_num
    $title, # tracks.title
    $genre; # tracks.title
    create a new element of the tracks hash using the value of the
    variable $filename as the key

    treat that new element as an array reference and push a bunch of
    values onto
    it

    assume for a secont that $filename has the value 'file1' .. at
    this point in
    the code you have something like the following structure

    %tracks = ( file1 => [ $_, 'some/path/', 'artist_name', 'etc...'
    ] );

    I didn't continue it .. but as well as the artist name you've got
    the album
    title, the track number, title, and genre in there .. you get the
    drift

    but you've got only one member of the %tracks hash .. it's key is
    the value
    of $filename and it's value is an array reference as above
    for my $row ( keys %tracks ){
    so now we grab the keys of %tracks - of which there is only one -
    'file1' in
    our example .. then we stick that into $row
    (my $qualified_filename, my $filename, my $filepath,
    $artist, $album, $tracknum, $title, $genre) =
    "$row @{ $tracks{$row} }";
    now we create a string containing the key ('file1') then a space
    and then the values of the array referred to by $tracks{file1}

    it's a single value - so it goes into the first element of the
    lvalue list -
    ie. $qualified_filename

    the following code would have done EXACTLY the same as what you've
    done above

    my %tracks;

    $tracks{$filename} = [ $_,
    $File::Find::dir . '/',
    $artist,
    $album,
    $tracknum,
    $title,
    $genre,
    ];

    my $qualified_name = "$filename @{ $tracks{$filename} }";

    so .. as you can tell - you'll have to explain a bit more about
    what you're
    trying to do .. because it's really not clear

    --
    jason king

    A Canadian law states that citizens may not publicly remove
    bandages. - http://dumblaws.com/
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
  • J. Patrick Lanigan at May 1, 2001 at 3:33 pm
    Excellent! Thanks! So, when I need to iterate through the hash of hashes and
    work on the data would I do this:

    for my $row ( keys %tracks ) {
    $artist = $tracks{$fname}->{artist};
    $title = $tracks{$fname}->{title};
    }

    Thanks again for the pointer,
    Patrick
    Gary Stainburn wrote:

    Surely, a hash of hashes would be better then.

    my %tracks=(); # create empty hash

    Then to populate the data you do something like:

    $tracks{$fname}->{artist}=$artist;
    $tracks{$fname}->{title}=$title;

    Gary
    On Tuesday 01 May 2001 4:08 pm, J. Patrick Lanigan wrote:
    Overall what I am trying to accomplish is to loop through a bunch of
    mp3 files and extract the ID3 Tag Info into a hash (%tracks) for
    which each key references the filename, filepath, artist, album,
    tracknum, title, genre for one file. Once I have %tracks populated I
    need to iterate through it and INSERT the info into a PostgreSQL DB.
    I am completely open to any other suggestion for a way to collect a
    dataset with a unique identifier. I've got all the other code
    working, this is the last piece to my puzzel.

    Thanks,
    Patrick
    Jason King wrote:

    J. Patrick Lanigan writes ..
    I haven't quite sorted out the more complex data structure in perl
    yet. Anyhow, I need to take the following hash of arrays...
    you have some very confusing code there .. so let's do it one bit
    at a time
    my %tracks = ();
    create a hash called tracks with zero elements
    push @{$tracks{$filename}},
    $_, # tracks.filename

    $File::Find::dir . '/', # tracks.filepath
    $artist, # artist.artist_name
    $album, # album.title
    $tracknum, # tracks.track_num
    $title, # tracks.title
    $genre; # tracks.title
    create a new element of the tracks hash using the value of the
    variable $filename as the key

    treat that new element as an array reference and push a bunch of
    values onto
    it

    assume for a secont that $filename has the value 'file1' .. at
    this point in
    the code you have something like the following structure

    %tracks = ( file1 => [ $_, 'some/path/', 'artist_name', 'etc...'
    ] );

    I didn't continue it .. but as well as the artist name you've got
    the album
    title, the track number, title, and genre in there .. you get the
    drift

    but you've got only one member of the %tracks hash .. it's key is
    the value
    of $filename and it's value is an array reference as above
    for my $row ( keys %tracks ){
    so now we grab the keys of %tracks - of which there is only one -
    'file1' in
    our example .. then we stick that into $row
    (my $qualified_filename, my $filename, my $filepath,
    $artist, $album, $tracknum, $title, $genre) =
    "$row @{ $tracks{$row} }";
    now we create a string containing the key ('file1') then a space
    and then the values of the array referred to by $tracks{file1}

    it's a single value - so it goes into the first element of the
    lvalue list -
    ie. $qualified_filename

    the following code would have done EXACTLY the same as what you've
    done above

    my %tracks;

    $tracks{$filename} = [ $_,
    $File::Find::dir . '/',
    $artist,
    $album,
    $tracknum,
    $title,
    $genre,
    ];

    my $qualified_name = "$filename @{ $tracks{$filename} }";

    so .. as you can tell - you'll have to explain a bit more about
    what you're
    trying to do .. because it's really not clear

    --
    jason king

    A Canadian law states that citizens may not publicly remove
    bandages. - http://dumblaws.com/
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
  • J. Patrick Lanigan at May 1, 2001 at 3:38 pm
    Opps, forgot to change the variable name. Ah the power of cut and paste...

    for my $row ( keys %tracks ) {
    $artist = $tracks{$row}->{artist};
    $title = $tracks{$row}->{title};
    }
    I wrote:

    Excellent! Thanks! So, when I need to iterate through the hash of
    hashes and
    work on the data would I do this:

    for my $row ( keys %tracks ) {
    $artist = $tracks{$fname}->{artist};
    $title = $tracks{$fname}->{title};
    }

    Thanks again for the pointer,
    Patrick
    Gary Stainburn wrote:

    Surely, a hash of hashes would be better then.

    my %tracks=(); # create empty hash

    Then to populate the data you do something like:

    $tracks{$fname}->{artist}=$artist;
    $tracks{$fname}->{title}=$title;

    Gary
    On Tuesday 01 May 2001 4:08 pm, J. Patrick Lanigan wrote:
    Overall what I am trying to accomplish is to loop through a bunch of
    mp3 files and extract the ID3 Tag Info into a hash (%tracks) for
    which each key references the filename, filepath, artist, album,
    tracknum, title, genre for one file. Once I have %tracks populated I
    need to iterate through it and INSERT the info into a PostgreSQL DB.
    I am completely open to any other suggestion for a way to collect a
    dataset with a unique identifier. I've got all the other code
    working, this is the last piece to my puzzel.

    Thanks,
    Patrick
  • J. Patrick Lanigan at May 1, 2001 at 3:50 pm
    Thank you so much for your help Gary and Jason. I tried the following and it
    works perfectly.

    Patrick

    #!c:/perl/bin/perl -w
    use strict;
    my %tracks=(); # create empty hash

    $tracks{'test'}->{artist}='test_artist';
    $tracks{'test'}->{title}='test_title';
    $tracks{'test2'}->{artist}='test_artist2';
    $tracks{'test2'}->{title}='test_title2';

    for my $row ( keys %tracks ) {
    my $artist = $tracks{$row}->{artist};
    my $title = $tracks{$row}->{title};
    print '\$row=', $row, "\n", '\$artist=', $artist, "\n", '\$title=', $title,
    "\n";
    }

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMay 1, '01 at 8:38a
activeMay 1, '01 at 3:50p
posts6
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase