Halbird has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm very new to this so please be gentle.

I wrote some (working) code using the qw function to list data from a file. Here's what's relevant:

my @SList = qw(Name Latitude Longitude);

I'm trying to write a foreach loop that will go over each line in the list and put the Latitude and Longitude into a subroutine which i've already written (also works).

My question is:

Have I made a mistake using the qw function? I dont see how I can extrapolate the Latitude/Longitude from my list as I've written it using 'quoted words', perhaps I should be using scalars.

I thought I could initialize scalars which uses the data that qw Latitude / Longitude lists, but I'm having no luck with this. Is it possible?

I hope this makes sense.

Replies are listed 'Best First'.
Re: (Beginner) Can 'qw' be implemented into a list?
by graff (Chancellor) on Mar 24, 2015 at 02:06 UTC
    Try posting some more code (like the subroutine you mentioned), and maybe some sample data from the relevant data file. The one line of code you showed is certainly sensible on its own - it creates an array that contains just three string values ("Name", "Latitude" and "Longitude").

    Now, if you have a data file containing lines of data, with perhaps 3 values per line, you might be wanting to load that into a some sort of data structure... perhaps an array of hashes... something like this?

    my @SList = qw(Name Latitude Longitude); my @AoH; while (<FILE>) { chomp; my %hash; @hash{@SList} = split; # left-hand-side is called a "hash slice" push @AoH, \%hash; }
Re: (Beginner) Can 'qw' be implemented into a list?
by GrandFather (Saint) on Mar 24, 2015 at 06:07 UTC

    See I know what I mean. Why don't you? to get a feel for what we want to see. Your description is unclear because you use words in an interesting way that doesn't fit with the way we expect. Very likely that all sounds fine in your head, but we haven't the technology to post your head to the site for examination by everyone which makes it hard to answer your question in a way that is likely to help.

    As others have suggested, show us your code, but first read the node linked above to help you focus on just what is needed to get the best help from us.

    Perl is the programming world's equivalent of English
Re: (Beginner) Can 'qw' be implemented into a list?
by LanX (Saint) on Mar 24, 2015 at 02:05 UTC
    plz show us your loop! Either you need to always take 3 elements from a flat array or you need a nested array of arrays.

    In any case use Data::Dumper (it's core) to inspect the content of @SList

    use Data::Dumper; print Dumper(\@SList)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

Re: (Beginner) Can 'qw' be implemented into a list?
by Anonymous Monk on Mar 24, 2015 at 01:59 UTC

    I don't understand the question. The way you use "implemented into" and "extrapolate " is confusing.

    So back to Basic debugging checklist

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my @foo = qw/ a b c /; dd( \@foo ); my( $ba, $bar, $rar ) = qw/ q r s /; dd( $ba, $bar, $rar ); __END__ ["a", "b", "c"] ("q", "r", "s")

    Does that help you ask clearer question? Does it answer any questions?

Re: (Beginner) Can 'qw' be implemented into a list?
by BrowserUk (Patriarch) on Mar 24, 2015 at 08:17 UTC
    I thought I could initialize scalars which uses the data that qw Latitude / Longitude lists, but I'm having no luck with this. Is it possible?

    My guess as to your meaning:

    my( $name, $lat, $long ) = qw(Name Latitude Longitude);

    Though it doesn't seem too useful.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: (Beginner) Can 'qw' be implemented into a list?
by Anonymous Monk on Mar 24, 2015 at 04:05 UTC

    Your question is not very clear, but I think what you want is an AoA (array of arrays)

    my @list; push @list, [qw(Name Latitude Longitude)]; #or my @list = [qw (Name Latitude Longitude)], [qw (Name Latitude Longitude)], [qw (Name Latitude Longitude)], etc ... );
Re: (Beginner) Can 'qw' be implemented into a list?
by hdb (Monsignor) on Mar 24, 2015 at 07:58 UTC

    Is this what you are looking for?

    use strict; use warnings; sub f { print "$_[0]\n"; } my @SList = qw(Name Latitude Longitude); foreach my $item (@SList) { f( $item ); }
Re: (Beginner) Can 'qw' be implemented into a list?
by jeffa (Bishop) on Mar 24, 2015 at 18:28 UTC

    Unfortunately, you have edited your code TOO much. We really need to see more to understand exactly what you are trying to do. The questions arise because you don't seem to have real values in your @SList array. Those appear to be headers that could be used in a Two Dimensional array or they appear to be keys that could be used in an Array of Hashes. You really need to provide more code and more context.

    Additionally, there seems to be some confusion on your end about what qw() is used for -- you question whether or not you should be using scalars instead, without realizing that you already are using scalars. qw() is merely a convenience function that allows us lazy programmers to leave out quotations and commas when creating hard coded arrays, like so:

    my @hard2type = ( 'one', 'two', 'three', 'four' ); my @ez2type = qw( one two three four );

    Finally, perhaps you can elaborate what you mean when you said "... write a foreach loop that will go over each line in the list ..." because as it stands, you have only presented one line, not many. Thanks!

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: (Beginner) Can 'qw' be implemented into a list?
by Discipulus (Canon) on Mar 24, 2015 at 09:39 UTC
    Hello Halbird,
    follow the wise advices from other monks, first of all on 'how ask a question'..

    To strictly stick with with your question, as you present as a newbie, i like to address you to the basic concepts: qw is a Quote like Operator.
    qw/STRING/ Evaluates to a list of the words extracted out of STRING, using embedd +ed whitespace as the word delimiters. It can be understood as being r +oughly equivalent to: split(" ", q/STRING/); the differences being that it generates a real list at compile time, a +nd in scalar context it returns the last element in the list. So this + expression: qw(foo bar baz) is semantically equivalent to the list: "foo", "bar", "baz"
    So qw is used (mostly) in list context to return a list, and yes you can assign a list to an array. Crucial concepts are: list and context. Basicly a list is an UNordered sequence of values. A list is like a primitive idea in Perl. Lists cannot be accessed using an index. Array are indexed lists so you can access one element by the mean of its index. Look for context at modernperl and obviously at perlmonks.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      ... a list is an UNordered sequence of values. ... Lists cannot be accessed using an index.

      I disagree with both these assertions. To my way of thinking, lists are most certainly ordered. In what sense do you think of them otherwise? They can also be array indexed:

      c:\@Work\Perl>perl -wMstrict -le "my $s = ('foo', 'bar', 'baz')[1]; print qq{'$s'}; ;; $s = (qw(zip zap zop))[1]; print qq{'$s'}; ;; $s = qw(hic hac hoc)[1]; print qq{'$s'}; " 'bar' 'zap' 'hac'
      Note: I think the  qw(hic hac hoc)[1] example relies on a syntactic feature that, if not deprecated, is at least disparaged and contumed, and may be slated to go away someday/someversion (I'm running this with 5.14). It is certainly deprecated (as of 5.14) in a for-loop:
      c:\@Work\Perl>perl -wMstrict -le "for my $s qw(hic hac hoc) { print qq{'$s'}; } " Use of qw(...) as parentheses is deprecated at -e line 1. 'hic' 'hac' 'hoc'


      Give a man a fish:  <%-(-(-(-<

        You are obviously right, thanks to point me in the right direction. I still find difficult to be precise describing Perl behaviour. My attempt was to tell the difference between lists and arrays and the OP confusion undeerstanding the @arr = qw (a b); construct.

        Anyway you too are wrong, because the latin demonstrative pronouns are: qw(hic haec hoc) and not qw(hic hac hoc) ...;=)

        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: (Beginner) Can 'qw' be implemented into a list?
by marinersk (Priest) on Mar 24, 2015 at 11:27 UTC

    Suspect one of these examples will get you going:

    #!/usr/bin/perl use strict; print "-----[ Method 1 ]----------------\n"; my @SList = qw(Name Latitude Longitude); foreach my $Item_From_SList (@SList) { print " Item found: $Item_From_SList\n"; } print "-----[ Method 2 ]----------------\n"; my ($name, $lat, $long) = qw(Name Latitude Longitude); print " Name of Name: $name\n"; print " Name of Lat: $lat\n"; print " Name of Long: $long\n"; print "-----[ Method Ends ]-------------\n"; exit; __END__

    Otherwise, see if you can expand on your question use more but smaller words. In my experience, that's the best way to arrive at a common language.

Re: (Beginner) Can 'qw' be implemented into a list?
by Halbird (Novice) on Mar 24, 2015 at 23:53 UTC
    Sorry. Let me be more clear about this: I have this code which splits a data file into headings (Im sure some of this is redundant for what I want to do):
    my @SList = qw(Name Latitude Longitude); my $FileA = "Mammal.txt"; open my $Mammals, "<", $FileA or die "Cannot open file'$FileA'."; while (my $line = <$Mammals>) { my %data; @data{@SList} = split /\t/, $line, scalar @SList; foreach my $species (@SList) { printf "%-10.10s : %s\n", $species, $data{$species}; } } close $Mammals;
    I also have this subroutine for calculating a distance:
    sub CalculateDistance($$$$) { my ($Lat1, $Lon1, $Lat2, $Lon2) = @_; my ($nDLat, $nDLon); my ($nA, $nC, $nD); $nDLat = ($Lat1 - $Lat2) * 0.017453293; $nDLon = ($Lon1 - $Lon2) * 0.017453293; $Lat1 = ($Lat1) * 0.017453293; $Lat2 = ($Lat2) * 0.017453293; $nA = (sin($nDLat/2) ** 2) + cos($Lat1) * cos($Lat2) * ( sin($nDLon/2 +) ** 2 ); $nC = 2 * atan2( sqrt($nA), sqrt( 1 - $nA )); $nD = 6372.797 * $nC; return $nD; }
    I have two values which I will be using in the CalculateDistance subroutine:
    CalculateDistance (54.988056, -1.619444, $Lat2, $Lon2);
    Example of data in my file which might help:
    Myotis daubentonii 52.12909504 -0.882318328 Myotis nattereri 53.90074722 -1.771685946 Myotis nattereri 54.08052026 -1.770700447 Myotis nattereri 53.54023273 -1.471802022 Eptesicus serotinus 51.40241395 0.250171498 Eptesicus serotinus 51.35533502 0.242835438 Eptesicus serotinus 51.0001581 0.351815969
    I want to use the latitude and longitude from the file and implement it into the CalculateDistance subroutine.
    Then I can create a counter which calculates how many data sets return a distance of more than x.
    Does this make any more sense? I appreciate everyone's advice, and although I read through all of your examples I think I explained myself so terribly that my question was misunderstood.

      How can you have code that looks like that, and use a phrase like "implement it into the CalculateDistance subroutine"? I guess you have found code somewhere that is doing almost what you want and you wish to bend it to fit? Something like:

      ++$count if CalculateDistance (54.988056, -1.619444, @data{@SList[ +1 .. 2]}) > $x;

      inside your while loop should be the trick.

      Perl is the programming world's equivalent of English