# Extracting elements from pmap using for loop

**URL:** <https://discourse.julialang.org/t/extracting-elements-from-pmap-using-for-loop/87516>\
**Category:** General Usage\
**Tags:** question, pmap\
**Created:** [September 20, 2022, 12:48pm UTC](https://discourse.julialang.org/t/extracting-elements-from-pmap-using-for-loop/87516 "2022-09-20T12:48:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mb96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mb96/32/45786_2.png) [@mb96](https://discourse.julialang.org/u/mb96)\
**Post date:** [September 20, 2022, 12:48pm UTC](https://discourse.julialang.org/t/extracting-elements-from-pmap-using-for-loop/87516/1 "2022-09-20T12:48:49Z")

</div>

I am confused about Julia behavior when I used pmap return object as an iterator. I will first show a chunk of code isomorphic to the malfunctioning code but which works and then show the malfunctioning code.

## Consider this code embeded within a function which takes as an argument a vector of Integers:

```
MEPS_all_years = DataFrame();
for year in years
    year_data = CSV.read(string("/users/miguelborrero/desktop/EffectACA/MEPS/augmented_MEPS", year, ".csv"), DataFrame)
    MEPS_all_years = vcat(MEPS_all_years, year_data, cols=:union)
end

```

* * *

## Now consider an “a priori” very similar (in terms of the use of the for loop) chunk of code which is also embedded in a function:

```
@everywhere function utility_gen(util::Int64, yearly_utility::DataFrame, hourly_utility::DataFrame, hourly_fuel_utility::DataFrame, parameters::Dict, weeklist::Vector{Vector{Int64}})

    fit_results = DataFrame(utility_id = Int64[], est_YEAR = Int64[], sample_hour_of_year = Int64[], modefuel_short = String[], gload = Float64[],
    elec_price = Float64[], util_revenues = Float64[], TVC = Float64[], NAMEPLATE = Float64[])

```

…  
…  
…  
return fit\_results

```
end

util_parallel(yearly_utility::DataFrame) = pmap(u -> utility_gen(u, yearly_utility, hourly_utility, hourly_fuel_utility, parameters, weeklist), [u for u in unique(yearly_utility.utility_id)])

fit_results_hourly = DataFrame()

for i in util_parallel(yearly_utility)
    fit_results_hourly = vcat(fit_results_hourly, i)
end

```

* * *

Supposedly, util\_parallel(yearly\_utility) is an array of DataFrames and hence it can work as an iterable object and it does but when I run this last part it raises the undefvar error, claiming that fit\_results\_hourly is not defined. This does not happen in the first case and so it seems like using a return object from pmap(.) as the iterable object is messing with the scope.

Can someone give some insight as to way I am getting this error?

Thanks a lot!
